您的位置:首页 > 产品设计 > UI/UE

UIScrollView 实现比例缩放

2015-07-14 21:27 513 查看
#import "RootViewController.h"

@interface RootViewController ()<UIScrollViewDelegate>
{
UIImageView *imageView;
UILabel *scaleRatioLabel;// 显示倍率用的Label
}
@property (nonatomic, strong)UIScrollView *scrollView;

@end

@implementation RootViewController

- (void)dealloc
{
self.scrollView = nil;
}

- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1-3.jpg"]];
[imageView setCenter:CGPointMake([UIScreen mainScreen].bounds.size.width/2.0, [UIScreen mainScreen].bounds.size.height/2.0)];
[self.scrollView addSubview:imageView];
//内容大小与图片大小一致
self.scrollView.contentSize = imageView.frame.size;
// 最小缩放比例
self.scrollView.minimumZoomScale = 0.2f;
// 最大缩放比例
self.scrollView.maximumZoomScale = 5.0f;

// 用来显示倍率的Label
scaleRatioLabel = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2.0 - 25, [UIScreen mainScreen].bounds.size.height/2.0 - 12.5, 50, 25)];
[scaleRatioLabel setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:scaleRatioLabel];

}

#pragma mark - UIScrollViewDelegate
// 设置要缩放的控件
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}

// 处理结束缩放事件
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
[self.view bringSubviewToFront:scaleRatioLabel];
[scaleRatioLabel setAlpha:0.6f];
[scaleRatioLabel setBackgroundColor:[UIColor lightGrayColor]];
scaleRatioLabel.text = [NSString stringWithFormat:@" x%.1f",scale];

[UIView transitionWithView:scaleRatioLabel duration:2.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
scaleRatioLabel.alpha = 0.0f;
} completion:nil];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: