1,先设置一个属性用来存放缩放数值,可以在 .h 文件中进行:
#import@interface ViewController : UIViewController@property (nonatomic) CGFloat lastScale ;@end
2,设置缩放手势代理协议:
#import "ViewController.h"@interface ViewController ()@property (nonatomic,strong) UIView *viewVC;@end
3,添加视图,并将视图添加手势:
@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; UIView *viewVC = [[UIView alloc]initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height - 300)]; self.viewVC = viewVC; UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height - 300)]; imgView.image = [UIImage imageNamed:@"Kuture"]; UIPinchGestureRecognizer *pinchGes = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(scaGesture:)]; [pinchGes setDelegate:self]; [self.viewVC addGestureRecognizer:pinchGes]; [self.viewVC addSubview:imgView]; [self.view addSubview:self.viewVC];}
4,设置缩放手势的方法实现:
-(void)scaGesture:(id)sender { [self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]]; if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) { _lastScale = 1.0; return; } CGFloat scale = 1.0 - (_lastScale - [(UIPinchGestureRecognizer*)sender scale]); CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform; CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale); [[(UIPinchGestureRecognizer*)sender view]setTransform:newTransform]; // NSLog(@"Scale:%f LastScale:%f ViewX:%f ViewY:%f ViewW:%f VeiwH:%f",scale,_lastScale,self.viewVC.bounds.origin.x,self.viewVC.bounds.origin.y,self.viewVC.bounds.size.width,self.viewVC.bounds.size.height); _lastScale = [(UIPinchGestureRecognizer*)sender scale]; }@end
*****测试所用图标*****
1> 知识库
2> EasyBug
3> 系统消息
4> 私信
5> 评论
6>我的