IOS學習之——Gesture手勢基礎

//
//  MyViewController.m
//  網絡NSUIrConnect
//
//

#import "MyViewController.h"

@interface MyViewController ()
@end
@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    
    //添加圖片
    UIImage *imge=[UIImage imageNamed:@"welcome1"];
    UIImageView *iv=[[UIImageView alloc]initWithImage:imge];
    iv.frame=CGRectMake(50, 80, 200, 300);
    [self.view addSubview:iv];
    //開啓交互事件響應
    iv.userInteractionEnabled=YES;
    //UITapGestureRecognizer點擊手勢類
    //功能:識別點擊手勢事件

    UITapGestureRecognizer *tapOne=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapOneAct:)];
    //表示收拾識別事件的事件類型:幾次點擊事件
    //默認值是1
    tapOne.numberOfTapsRequired=1;
    //表示幾個手指觸發此事件函數
    //默認值是1
    tapOne.numberOfTouchesRequired=1;
    //將點擊事件添加到視圖中,視圖即可響應事件
    [iv addGestureRecognizer:tapOne];
    
    
    UITapGestureRecognizer *tapTwo=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTwoAct:)];

    //雙擊事件
    tapTwo.numberOfTapsRequired=2;
    //表示幾個手指觸發此事件函數
    tapTwo.numberOfTouchesRequired=1;
    //將點擊事件添加到視圖中,視圖即可響應事件
    [iv addGestureRecognizer:tapTwo];
    //點擊雙擊時,單擊失效
    [tapTwo requireGestureRecognizerToFail:tapOne];
}

-(void)tapTwoAct:(UITapGestureRecognizer *) sender{
    NSLog(@"雙擊事件……");
    //獲取點擊的View
    UIImageView *iv=(UIImageView*)sender.view;
    //動畫開啓
    [UIView beginAnimations:nil context:nil];
    //動畫過渡時間
    [UIView setAnimationDuration:2];
    iv.frame=CGRectMake(50, 80, 400, 600);
    //動畫關閉
    [UIView commitAnimations];

}

-(void)tapOneAct:(UITapGestureRecognizer *) sender{
    NSLog(@"單擊事件……");
    //獲取點擊的View
    UIImageView *iv=(UIImageView*)sender.view;
    //動畫開啓
    [UIView beginAnimations:nil context:nil];
    //動畫過渡時間
    [UIView setAnimationDuration:2];
    iv.frame=CGRectMake(0, 0, 100, 150);
    //動畫關閉
    [UIView commitAnimations];
    
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章