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

手势操作(单击手势,长按手势,策划手势)

时间:2016-02-25 22:54:41      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

1.策划手势操作

//
//  ViewController.m
//  1-28策划手势
//
//  Created by ma c on 16/1/28.
//  Copyright © 2016年 bjsxt. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) UISwipeGestureRecognizer *recognizer;
@property (strong, nonatomic) UIButton *btn;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    
    self.btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 667)];
    [self.btn setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:self.btn];
    
    //一个手势只能对应一个,默认为右滑,左滑必须在创建一个

    self.recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)];
    
    [self.recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    
    [self.view addGestureRecognizer:self.recognizer];
    
    
    
}

- (void)handleSwipeFrom
{
    //如果往右滑
    if (self.recognizer.direction==UISwipeGestureRecognizerDirectionRight) {
       
        
        [UIView beginAnimations:@"1" context:nil];
        [UIView setAnimationDuration:0.5];
        
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        
        [UIView setAnimationRepeatAutoreverses:NO];
        
        self.btn.frame = CGRectMake(80, 0, 80, 667);
        
        [UIView commitAnimations];
    }
    

}

2.单击手势和长按手势操作
#import "ViewController.h"

@interface ViewController ()

@property (strong,nonatomic) UITextField *field;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //手势:滑动手势,点击手势,双击手势,长按手势,
    
    
    self.field=[[UITextField alloc] initWithFrame:CGRectMake(80, 211, 114, 30)];
    self.field.placeholder=@"请输入内容";
    
    //注意没设置代理
    [self.view addSubview:self.field];
    
    
    //创建单击手势UITapGestureRecognizer
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(wantdosomething)];
   //我们创建了一个手势,但是并没有吧手势放在具体的哪个控件上,也就是说:手势必须依托与控件,当具体某个控件上面放了个手势,这个手势才能响应
    
    //给当前视图添加单击手势,(把单击手势放在view上)那么用户在当前视图上单击的时候,就会执行下面的手势关联方法
    [self.view addGestureRecognizer:tap];
    
    
    //创建长按手势
    UILongPressGestureRecognizer *longpress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longpresswant)];
    longpress.minimumPressDuration=2.0;//最小时间为2s以上
    [self.view addGestureRecognizer:longpress];
    
    
    
    
    

    
}
//这个方法会在手势被触发的时候被调用.
- (void)wantdosomething
{
    NSLog(@"点击啦");
    [self.field resignFirstResponder];
}


- (void)longpresswant
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"触发动作" message:@"长按手势" delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
    [alert show];
}

 


@end

 

手势操作(单击手势,长按手势,策划手势)

标签:

原文地址:http://www.cnblogs.com/hongfuqing/p/5218486.html

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