添加手势动作

注意:(三个手指实现拖拽功能,按住option按钮和三个手指实现旋转功能)

首先创建一个single类型的类

在根视图控制器的.h文件中添加以下代码

以下程序适合x cold4.6.3

#import <UIKit/UIKit.h>


@interface liViewController : UIViewController


@property (retain,nonatomic) UIView *aNewView;


@end

在.m文件中添加以下代码

@implementation liViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor yellowColor];

    UIView *tempView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 120, 120)];

    tempView.backgroundColor = [UIColor purpleColor];

    self.aNewView = tempView;

    [tempView release];

    [self.view addSubview:self.aNewView];

    

    //添加各种手势以及其关联动作

    //1、添加单击手势

    UITapGestureRecognizer *singletap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleGesture:)];

    [self.view addGestureRecognizer:singletap];

    [singletap release];

    

    

    //添加双击手势 如果什么也不写下边新的对象以及关联的动作会把前边的动作覆盖住,不管单击还是双击都会运行后一个执行的动作

    UITapGestureRecognizer *doubletap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleGesture:)];

    //为了做标识,证明是双击 为了和上边的单击操作做区别

    doubletap.numberOfTapsRequired = 2;

    //如果没有这句话会同时执行单击和双击操作

    [singletap requireGestureRecognizerToFail:doubletap];

    [self.view addGestureRecognizer:doubletap];

    [doubletap release];

    

    

    //添加长按手势

    UILongPressGestureRecognizer *longtap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressed:)];

    //设置长按的时间是两秒

    longtap.minimumPressDuration = 2;

    [self.view addGestureRecognizer:longtap];

    [longtap release];

    

    //添加扫除手势

    UISwipeGestureRecognizer *swiptap = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipGesture:)];

    swiptap.direction = UISwipeGestureRecognizerDirectionLeft;

    [self.view addGestureRecognizer:swiptap];

    [swiptap release];

    

    //添加平移的手势

    UIPanGestureRecognizer *translationTap = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(translation:)];

    [self.view addGestureRecognizer:translationTap];

    [translationTap release];

    

    //添加旋转的手势

    UIRotationGestureRecognizer *rotationTap = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];

    [self.view addGestureRecognizer:rotationTap];

    [rotationTap release];

}

//单击关联的响应

- (void)singleGesture:(UITapGestureRecognizer *)tap

{

    NSLog(@"单击");

}

//双击关联的响应

- (void)doubleGesture:(UITapGestureRecognizer *)tap

{

    NSLog(@"双击");

}

//长按

- (void)longPressed:(UILongPressGestureRecognizer *)pressed

{

    NSLog(@"长按");

}

//清除手势关联的响应

- (void)swipGesture:(UISwipeGestureRecognizer *)swip

{

    NSLog(@"清除");

    

}

//平移手势对应的响应动作

- (void)translation:(UIPanGestureRecognizer *)pan

{

    CGPoint point = [pan locationInView:self.view];

    self.aNewView.center = point;

    NSLog(@"%@",NSStringFromCGPoint(point));

   // NSLog(@"平移");

}

//旋转手势对应的响应动作

- (void) rotation:(UIRotationGestureRecognizer *)revolve

{

    NSLog(@"旋转");

    float degree = revolve.rotation * (180/M_PI);

    NSLog(@"旋转的角度是%f",degree);

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (void)dealloc

{

    [_aNewView release];

    [super dealloc];

}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值