UIGestureRecognizer学习之—— UIRotationGestureRecognizer

来源:互联网 发布:淘米网络私有化 编辑:程序博客网 时间:2024/05/16 14:34

UIRotationGestureRecognizer是UIGestureRecognizer的子类。
UIRotationGestureRecognizer除了继承UIGestureRecognizer的属性外,还提供如下俩个属性:

rotation: //获取旋转角度velocity: //获取旋转速度

示例:通过旋转手势旋转图片

@implementation FKViewControllerUIImage* srcImage;CGFloat currentScale;CGFloat currentRotation;- (void)viewDidLoad{    [super viewDidLoad];    [UIApplication sharedApplication].statusBarHidden = YES;    srcImage = [UIImage imageNamed:@"seashore.png"];    // 设置图片直接显示在中间(不进行任何缩放)    self.view.contentMode = UIViewContentModeCenter;    // 设置imageView初始显示的图片    self.imageView.image = srcImage;    // 设置初始的缩放比例    currentScale = 1;    currentRotation = 0;    // 设置imageView允许用户交互,支持多点触碰    self.imageView.userInteractionEnabled = YES;    self.imageView.multipleTouchEnabled = YES;    // 创建UIPinchGestureRecognizer手势处理器,该手势处理器激发scaleImage:方法    UIPinchGestureRecognizer* gesture = [[UIPinchGestureRecognizer alloc]        initWithTarget:self action:@selector(scaleImage:)];    // 为imageView添加手势处理器    [self.imageView addGestureRecognizer:gesture];    // 创建UIRotationGestureRecognizer手势处理器,该手势处理器激发rotateImage:方法    UIRotationGestureRecognizer* rotateGesture =        [[UIRotationGestureRecognizer alloc]        initWithTarget:self action:@selector(rotateImage:)];    // 为imageView添加手势处理器    [self.imageView addGestureRecognizer:rotateGesture];}- (void) scaleImage:(UIPinchGestureRecognizer*)gesture{    CGFloat scale = gesture.scale;    // 根据手势处理器的缩放比计算图片缩放后的目标大小    CGSize targetSize = CGSizeMake(srcImage.size.width * scale * currentScale,        srcImage.size.height * scale * currentScale);    // 对图片进行缩放、旋转    self.imageView.image = [[srcImage imageByScalingToSize:targetSize]        imageRotatedByRadians:currentRotation];    // 如果手势结束    if(gesture.state == UIGestureRecognizerStateEnded)    {        // 计算结束时候图片的缩放比        currentScale = scale * currentScale;    }}- (void) rotateImage:(UIRotationGestureR- ecognizer*)gesture{    // 获取手势旋转的弧度    CGFloat rotation = gesture.rotation;    // 根据当前缩放比计算图片缩放后的目标大小    CGSize targetSize = CGSizeMake(srcImage.size.width * currentScale,                                   srcImage.size.height * currentScale);    // 对图片进行缩放、旋转    self.imageView.image = [[srcImage imageByScalingToSize:targetSize]        imageRotatedByRadians:currentRotation + rotation];    // 如果旋转手势结束    if(gesture.state == UIGestureRecognizerStateEnded)    {        currentRotation = currentRotation + rotation;    }}@end
1 0
原创粉丝点击