Objective c 制作放大&;带UIScrollView的out视图
我正在尝试使用UIScrollView进行一些放大和缩小视图。 就像第一次点击使视图放大3倍,再点击一次使视图再次放大3倍,然后下一次点击将视图缩小回原始大小。 但是我的代码不能正常工作。 这个怎么了? 谢谢Objective c 制作放大&;带UIScrollView的out视图,objective-c,uiscrollview,zooming,Objective C,Uiscrollview,Zooming,我正在尝试使用UIScrollView进行一些放大和缩小视图。 就像第一次点击使视图放大3倍,再点击一次使视图再次放大3倍,然后下一次点击将视图缩小回原始大小。 但是我的代码不能正常工作。 这个怎么了? 谢谢 #导入“ViewController.h” @界面视图控制器() @属性(强的,非原子的)IBMOutlet UIScrollView*myScrollView; @属性(强,非原子)IBUIImageView*myImageView; @结束 @实现视图控制器 -(无效)viewDidL
#导入“ViewController.h”
@界面视图控制器()
@属性(强的,非原子的)IBMOutlet UIScrollView*myScrollView;
@属性(强,非原子)IBUIImageView*myImageView;
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
self.myScrollView.delegate=self;
self.myScrollView.minimumZoomScale=1;
self.myScrollView.maximumZoomScale=8;
self.myScrollView.scrollEnabled=是;
self.myScrollView.showshorizontalscrolindicator=YES;
self.myScrollView.showshorizontalscrolindicator=YES;
UITapGestureRecognizer*doubleTap手势=[[UITapGestureRecognizer alloc]initWithTarget:self-action:@selector(doubleTap:)];
DoubleTapstore.numberOfTapsRequired=2;
self.myImageView.userInteractionEnabled=是;
[self.myImageView添加手势识别器:双击手势];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(无效)双击:(UITapgestureRecognitor*)手势
{
if(self.myScrollView.zoomScale
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIScrollView *myScrollView;
@property (strong, nonatomic) IBOutlet UIImageView *myImageView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myScrollView.delegate = self;
self.myScrollView.minimumZoomScale = 1;
self.myScrollView.maximumZoomScale = 8;
self.myScrollView.scrollEnabled = YES;
self.myScrollView.showsHorizontalScrollIndicator = YES;
self.myScrollView.showsHorizontalScrollIndicator = YES;
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
doubleTapGesture.numberOfTapsRequired = 2;
self.myImageView.userInteractionEnabled = YES;
[self.myImageView addGestureRecognizer:doubleTapGesture];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)doubleTap:(UITapGestureRecognizer *)gesture
{
if (self.myScrollView.zoomScale < self.myScrollView.maximumZoomScale){
float newScale = self.myScrollView.zoomScale * 3;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gesture locationInView:gesture.view]];
[self.myScrollView zoomToRect:zoomRect animated:YES];
}else{
[self.myScrollView setZoomScale:1.0 animated:YES];
}
}
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center
{
CGRect zoomRect;
zoomRect.size.height = [self.myScrollView frame].size.height * scale;
zoomRect.size.width = [self.myScrollView frame].size.width * scale;
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.width / 2.0);
return zoomRect;
}
#pragma mark - delegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.myImageView;
}
@end
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center
{
CGRect zoomRect;
zoomRect.size.height = [self.myScrollView frame].size.height / scale;
zoomRect.size.width = [self.myScrollView frame].size.width / scale;
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.width / 2.0);
return zoomRect;
}