码迷,mamicode.com
首页 > 其他好文 > 详细

手势图的设计原理(2)拖拽、捏合、轻扫、旋转

时间:2015-12-29 19:07:14      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

ViewController.m
#import "ViewController.h"

@interface ViewController ()
{
    UIImageView *imageView;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
   
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(200, 200, 200, 200)];
    imageView.image = [UIImage imageNamed:@"涂涂.jpg"];
    [self.view addSubview:imageView];
    /*
    手势又分为六大手势:
    六大手势  全部都继承自  UIGestureRecognizer
    1、点击 UITapGestureRecognizer
    2、长按 UILongPressGestureRecognizer
    3、拖拽 UIPanGestureRecognizer
    4、捏合 UIPinchGestureRecognizer
    5、轻扫 UISwipeGestureRecognizer
    6、旋转 UIRotationGestureRecognizer
    
     //UIGestureRecognizer
     //初始化手势
     //- (instancetype)initWithTarget:(nullable id)target action:(nullable SEL)action;
     //UIView 中有添加手势的方法
     //addGestureRecognizer:
     [xx addGestureRecognizer: xx];
    */
#pragma mark---------拖拽---------------------------
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    //设置最小的手指个数
    pan.minimumNumberOfTouches = 1;
    //设置最大的手指个数
    pan.maximumNumberOfTouches = 2;
    [self.view addGestureRecognizer:pan];
   
   
#pragma mark---------轻扫---------------------------
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    swipe.numberOfTouchesRequired = 1;
   
    //设置轻扫的方向
    /*
    
     UISwipeGestureRecognizerDirectionRight
     UISwipeGestureRecognizerDirectionLeft
     UISwipeGestureRecognizerDirectionUp
     UISwipeGestureRecognizerDirectionDown
    
     */
   
    //让轻扫的方向是右
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipe];
        //不能响应 手势冲突
    //等一个手势结束之后再去响应另外一个手势
    //等轻扫(swipe)响应之后再去响应拖拽(pan)
    [pan requireGestureRecognizerToFail:swipe];
   
    //让轻扫的方向是左
    UISwipeGestureRecognizer *left = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    left.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:left];
    [pan requireGestureRecognizerToFail:left];
   
#pragma mark---------捏合---------------------------
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
    [self.view addGestureRecognizer:pinch];
   
#pragma mark---------旋转---------------------------
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
    [self.view addGestureRecognizer:rotation];
}

//旋转
-(void)rotation:(UIRotationGestureRecognizer *)sender
{
    //获得手势的旋转角度,让imageView按照这个角度去变化
    imageView.transform = CGAffineTransformMakeRotation(sender.rotation);

}

//捏合
-(void)pinch:(UIPinchGestureRecognizer *)sender
{
    //是一个视图变形,transform是UIView里面的一个属性,可是使视图发生形态上的改变,变形之后的视图,做其他操作不会还原原来的形态,除非用transfrom里面的还原视图方法
   
    //@property(nonatomic) CGAffineTransform transform;   // default is CGAffineTransformIdentity. animatable
    //    CGAffineTransform 让视图变形的类
    //    CGAffineTransformMakeScale(<#CGFloat sx#>, <#CGFloat sy#>)  让视图按照一个比例去变化 -> 放大缩小
    //    CGAffineTransformMakeRotation(<#CGFloat angle#>) 让视图 按照一个弧度去变化 -> 用于旋转
    //    CGAffineTransformIdentity -> 还原之前改变的所有形态

    imageView.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);
}


//轻扫
-(void)swipe:(UISwipeGestureRecognizer *)sender
{
//    CGFloat x;
//    if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
//        x = 0.0;
//    }else{
//        x = 200.0;
//    }
    CGFloat x = sender.direction == UISwipeGestureRecognizerDirectionLeft?0:200;
   
    [UIView animateWithDuration:0.7 animations:^{
        self.view.frame = CGRectMake(x, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(30, 50, 60, 40)];
        [button  setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [self.view addSubview:button];
    }];
   

}

//拖拽
-(void)pan:(UIPanGestureRecognizer *)sender
{
    //让视图还原成初始样式
    imageView.transform = CGAffineTransformIdentity;
//    CGPoint point = [sender translationInView:self.view];
//    NSLog(@"x:%f y:%f",point.x,point.y);
    //point 点击位置是(0,0)平移之后向左减少,向上减少,可以获得的方向和位置translationInView:
   
    //获得拖动的中心点
    CGPoint panCenter = [sender locationInView:self.view];
    imageView.center = panCenter;

}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

手势图的设计原理(2)拖拽、捏合、轻扫、旋转

标签:

原文地址:http://www.cnblogs.com/liuzhi20101016/p/5086498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!