iOS UIGestureRecognize(手势放大 缩小 旋转 功能)

//联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄

/**

注意点: 1.看 GIF 效果图.

2.看连线视图的效果图.

3.看实现代码(直接复制实现效果).

*/

一、GIF 效果图:

二、连线视图的效果图:

图1:

iOS UIGestureRecognize(手势放大 缩小 旋转 功能)_第1张图片

三、实现代码:

=======

================================

=======

控制器1:

//  ViewController.m

//手势dome

//

//  Created by石虎on 2017/7/26.

//  Copyright © 2017年shihu. All rights reserved.

//

#import"ViewController.h"

@interfaceViewController()

@property(weak,nonatomic)IBOutletUIImageView*imageView;

@property(nonatomic)CGFloatlastRotation;//最后一次旋转

@end

/**

UIGestureRecognize的子类类别有以下几种:

UITapGestureRecognizer            //轻拍识别器

UIPinchGestureRecognizer         //捏合识别器

UIRotationGestureRecognizer     //旋转识别器

UISwipeGestureRecognizer       //扫动识别器

UIPanGestureRecognizer        //拖动识别器

UILongPressGestureRecognizer //长按识别器

下面的例子我是用UIPinchGestureRecognizer和UIRotationGestureRecognizer通过手势实现图片旋转和缩小放大。

*/

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

//添加捏合手势识别器,changeImageSize:方法实现图片的放大与缩小

UIPinchGestureRecognizer*pinchRecognizer = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(changeImageSize:)];

[self.viewaddGestureRecognizer:pinchRecognizer];

//添加旋转手势识别器,rotateImage:方法实现图片的旋转

UIRotationGestureRecognizer*rotateRecognizer = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotateImage:)];

[self.viewaddGestureRecognizer:rotateRecognizer];

}

#pragma mark --捏合手势识别器

-(void)changeImageSize:(UIPinchGestureRecognizer*)recognizer

{

CGRectframe =self.imageView.frame;

//监听两手指滑动的距离,改变imageView的frame

frame.size.width= recognizer.scale*128;

frame.size.height= recognizer.scale*128;

self.imageView.frame= frame;

//保证imageView中心不动

self.imageView.center=CGPointMake(self.view.frame.size.width/2,self.view.frame.size.height/2);

}

#pragma mark -旋转手势识别器监听

-(void)rotateImage:(UIRotationGestureRecognizer*)rotateRecognizer

{

//改变imageView的角度,使图片旋转

if([rotateRecognizerstate]==UIGestureRecognizerStateEnded) {

self.lastRotation=0.0;

return;

}

CGAffineTransformcurrentTransform =self.imageView.transform;

CGFloatrotation =0.0- (self.lastRotation- rotateRecognizer.rotation);

//拿到当前的位置和尺寸

CGAffineTransformnewTransform =CGAffineTransformRotate(currentTransform,rotation);

self.imageView.transform= newTransform;

//显示最后一次的位置

self.lastRotation= rotateRecognizer.rotation;

}

@end

=======

================================

=======

谢谢!!!

你可能感兴趣的:(iOS UIGestureRecognize(手势放大 缩小 旋转 功能))