固定范围内拖动缩放一个图片 借鉴了github中的ImageZoom
@interface ViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView.delegate = self;
self.scrollView.minimumZoomScale = 1;
self.scrollView.maximumZoomScale = 3;
self.scrollView.bounces = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
NSArray * arr = @[@"亚洲",@"欧洲",@"非洲",@"北美洲",@"南美洲",@"大洋洲",@"南极洲"];
for (NSInteger i = 0; i<7; i++) {
UIButton *button = [[UIButton alloc]init];
[self.imageView addSubview:button];
button.titleLabel.font=[UIFont fontWithName:@"Bodoni 72 Smallcaps" size:15];
[button setTitle:arr[i] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"bg_lastminute_view"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"x_myOrder_green"] forState:UIControlStateSelected];
[button setTitleColor:[UIColor colorWithWhite:0.400 alpha:1.000] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
button.tag=i+100;
if (i==0) {
button.frame = CGRectMake(W - 95, 50, 40, 25);
}
if (i == 1) {
button.frame = CGRectMake(150, 40, 40, 25);
}
if (i == 2) {
button.frame = CGRectMake(190, 100,40, 25);
}
if (i == 3) {
button.frame = CGRectMake(40, 50, 50, 25);
}
if (i == 4) {
button.frame = CGRectMake(80, 120, 50, 25);
}
if (i == 5) {
button.frame = CGRectMake(W - 80,110, 50, 25);
}
if (i == 6) {
button.frame = CGRectMake(W - 115, 170, 50, 25);
}
}
UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 125, 40, 40)];
NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"shanshuo1"],
[UIImage imageNamed:@"shanshuo2"],
[UIImage imageNamed:@"shanshuo3"],
[UIImage imageNamed:@"shanshuo4"],
[UIImage imageNamed:@"shanshuo5"],
[UIImage imageNamed:@"shanshuo6"],
nil];
gifImageView.animationImages = gifArray; //动画图片数组
gifImageView.animationDuration = 2; //执行一次完整动画所需的时长
gifImageView.animationRepeatCount = 0; //动画重复次数
[gifImageView startAnimating];
[self.imageView addSubview:gifImageView];
UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
tap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tap];
}
static CGFloat scale = 1.5;
static bool isBig = NO;
static CGPoint imagePoint;
static CGSize size;
- (void)tap:(UITapGestureRecognizer *)tap{
imagePoint = [tap locationInView:self.imageView];
if(isBig == NO)
{
if(CGRectContainsPoint(self.imageView.bounds, imagePoint)){
size = self.imageView.size;
// 向上、向左偏移的距离
CGFloat top,left;
top = imagePoint.y * (scale - 1);
left = imagePoint.x * (scale - 1);
[UIView animateWithDuration:0.5 animations:^{
self.imageView.frame = CGRectMake(-left, -top,size.width * scale , size.height * scale);
}];
self.scrollView.contentSize = CGSizeMake(size.width * scale , size.height * scale);
self.scrollView.contentInset = UIEdgeInsetsMake(top, left, -top, -left);
isBig = YES;
}
}else{
[UIView animateWithDuration:.5f animations:^{
self.scrollView.contentOffset = CGPointMake(0, 0);
self.imageView.frame = CGRectMake(0, 0, size.width, size.height);
}];
self.scrollView.contentInset = UIEdgeInsetsMake(0,0,0,0);
self.scrollView.contentSize = size;
isBig = NO;
}
}
效果图如下