首页 > 移动平台 > 详细

iOS基本手势

时间:2015-04-06 00:42:41      阅读:288      评论:0      收藏:0      [点我收藏+]
#import "TouchViewController.h"

@interface TouchViewController ()

@end

@implementation TouchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    
    NSArray *imageArr = [NSArray arrayWithObjects:@"CyanSquare",@"MagentaSquare",@"YellowSquare", nil];
    for (int i = 0; i < 3; i ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10+90*i, 50+110*i, 100, 100)];;
        imageView.image = [UIImage imageNamed:imageArr[i]];
        imageView.userInteractionEnabled = YES;
        [self.view addSubview:imageView];
        
        // 添加点击手势
        [self addGesture:imageView];
        [imageView release];
    }
    
    
}
// 添加手势识别器
- (void)addGesture:(UIImageView *)imageView
{
    // 1、添加单击事件,点击手势识别器
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
    // 设置点击的操作
    tap.numberOfTapsRequired = 2;
    // 将手势添加到view 上
    [imageView addGestureRecognizer:tap];
    [tap release];
    
//    // 2、创建一个移动手势
//    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
//    [imageView addGestureRecognizer:pan];
//    [pan release];
    
    // 3、 实现缩放,捏合手势
    UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinClick:)];
    [imageView addGestureRecognizer:pin];
    [pin release];
    
    // 4、旋转
    UIRotationGestureRecognizer *rota = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rota:)];
    [imageView addGestureRecognizer:rota];
    [rota release];
    // 5、长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longpress:)];
    [imageView addGestureRecognizer:longPress];
    [longPress release];
    
    // 6-1、轻扫手势,一个手势只能支持一个手势,要想支持多个,就得重新创建多个方向的
    // 会和移动的手势冲突
    UISwipeGestureRecognizer *swip1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    swip1.direction = UISwipeGestureRecognizerDirectionUp ;
    [imageView addGestureRecognizer:swip1];
    [swip1 release];
    // 6-2、轻扫手势
    UISwipeGestureRecognizer *swip2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    swip2.direction = UISwipeGestureRecognizerDirectionDown ;
    [imageView addGestureRecognizer:swip2];
    [swip2 release];
    // 6-3、轻扫手势
    UISwipeGestureRecognizer *swip3 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    swip3.direction = UISwipeGestureRecognizerDirectionLeft ;
    [imageView addGestureRecognizer:swip3];
    [swip3 release];
    // 6-4、轻扫手势
    UISwipeGestureRecognizer *swip4 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swip:)];
    swip4.direction = UISwipeGestureRecognizerDirectionRight ;
    [imageView addGestureRecognizer:swip4];
    [swip4 release];
}

// 实现手势的事件,只有点击图片的时候才会触发
- (void)tapClick:(UITapGestureRecognizer *)sender
{
    [self.view bringSubviewToFront:sender.view];
}

// 实现移动手势,类似于touchMove
- (void)move:(UIPanGestureRecognizer *)sender
{
    // 固定的写法
    CGPoint point = [sender translationInView:self.view];
    sender.view.center = CGPointMake(sender.view.center.x + point.x, sender.view.center.y + point.y);
    [sender setTranslation:CGPointZero inView:self.view];
}

// 实现捏合手势的功能

- (void)pinClick:(UIPinchGestureRecognizer *)sender
{
    sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
    sender.scale = 1;
    
}
// 实现旋转
- (void)rota:(UIRotationGestureRecognizer *)sender
{
    sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
    sender.rotation = 1;
}

 

iOS基本手势

原文:http://www.cnblogs.com/liudongyan/p/4395051.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!