(素材源码)猫猫学IOS(三十六)UI之手势事件旋转_缩放_拖拽

猫猫分享,必须精品

原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243?viewmode=contents
源码:http://download.csdn.net/detail/u013357243/8671943

效果

完成一个图片的捏合缩放,拖拽,旋转动作。

技术分享

代码:NYViewController.m

//
//  NYViewController.m
//  旋转_缩放_拖拽
//
//  Created by apple on 15-5-7.
//  Copyright (c) 2015年 znycat. All rights reserved.
//

#import "NYViewController.h"

#import "NYViewController.h"

@interface NYViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *iconView;

@end

@implementation NYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    //捏合
    [self pichTest];
    //旋转
    [self rotationTest];
    //拖拽
    [self panTest];
}

//拖拽
-(void)panTest
{
    UIPanGestureRecognizer  *pan = [[UIPanGestureRecognizer alloc] init];
    [self.iconView addGestureRecognizer:pan];

    [pan addTarget:self action:@selector(panView:)];
}

- (void)panView:(UIPanGestureRecognizer *)pan
{
    // 返回的值是以手指按下的点为原点
    // 1 2 3 4 5
    CGPoint point = [pan translationInView:pan.view];

    NSLog(@"拖拽事件 %@", NSStringFromCGPoint(point));
    CGPoint temp = self.iconView.center;

    temp.x += point.x;
    temp.y += point.y;
    self.iconView.center = temp;

    // 理解不了就记住就OK
    [pan setTranslation:CGPointZero inView:pan.view];
}

// 该方法返回的BOOL值决定了view是否能够同时响应多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    NSLog(@"%@ - %@", gestureRecognizer.class, otherGestureRecognizer.class);
    return YES;
}


- (void)pichTest
{
    // 捏合手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] init];
    pinch.delegate = self;

    [self.iconView addGestureRecognizer:pinch];
    [pinch addTarget:self action:@selector(pinchView:)];
}


- (void)pinchView:(UIPinchGestureRecognizer *)pinch
{
    //    NSLog(@"捏合事件 %.1f", pinch.scale);
    //    self.iconView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
    // 1.0 * 0.9
    self.iconView.transform = CGAffineTransformScale(self.iconView.transform, pinch.scale, pinch.scale);

    pinch.scale = 1.0;
}

- (void)rotationTest
{
    // 旋转
    UIRotationGestureRecognizer *gesture = [[UIRotationGestureRecognizer alloc] init];
    gesture.delegate = self;

    [self.iconView addGestureRecognizer:gesture];
    [gesture addTarget:self action:@selector(rotationView:)];
}

- (void)rotationView:(UIRotationGestureRecognizer *)gesture
{
    //    NSLog(@"旋转事件 %.1f", gesture.rotation);


    //    每次从最初的位置开始
    //    self.iconView.transform = CGAffineTransformMakeRotation(gesture.rotation);

    //    在传入的transform基础上递增一个弧度
    self.iconView.transform = CGAffineTransformRotate(self.iconView.transform, gesture.rotation);
    // 将旋转的弧度清零(注意不是将图片旋转的弧度清零, 而是将当前手指旋转的弧度清零)
    gesture.rotation = 0;// 如果理解不了 , 记住就OK
}

@end

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。