你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS開發從入門到精通--UIGesture手勢基礎

iOS開發從入門到精通--UIGesture手勢基礎

編輯:IOS開發綜合

UIGesture手勢基礎
這裡寫圖片描述

#import 

@interface ViewController : UIViewController{

    //定義一個視圖對象
    UIImageView * _imageView;
}


@end
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //加載圖像對象,從本地加載到內存
    UIImage * image =[UIImage imageNamed:@"17_2.png"];

    //創建圖像視圖
    _imageView = [[UIImageView alloc]init];

    //將圖像視圖的圖像賦值
    _imageView.image = image;
    _imageView.frame =CGRectMake(50, 80, 200, 300);

    [self.view addSubview:_imageView];


    //開啟交互事件響應開關
    //YES:可以響應交互事件
    //NO:不能接受響應事件,默認值為NO;
    _imageView.userInteractionEnabled=YES;

    //創建一個點擊手勢對象
    //UITapGestureRecognizer:點擊手勢類
    //功能:識別點擊手勢事件
    //p1:響應事件的擁有者對象,self表示當前視圖控制器
    //p2:響應事件的函數
    UITapGestureRecognizer * tapOneGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapOneAct:)];

    //表示手勢識別事件的事件類型:幾次點擊時觸發
    //默認值為1
    tapOneGes.numberOfTapsRequired=1;
//    tapOneGes.numberOfTapsRequired=2;

    //表示幾個手指點擊時觸發此事件函數
    //默認值為1
    tapOneGes.numberOfTouchesRequired=1;

    //將點擊事件添加到視圖中,視圖即可響應事件
    [_imageView addGestureRecognizer:tapOneGes];

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

    tapTwoGes.numberOfTapsRequired=2;
    tapTwoGes.numberOfTouchesRequired=1;
    [_imageView addGestureRecognizer:tapTwoGes];

    //當單擊操作遇到雙擊操作時,單擊操作失效
    [tapOneGes requireGestureRecognizerToFail:tapTwoGes];
}

//事件響應函數,單擊操作
//參數手勢點擊事件對象
-(void) tapOneAct:(UITapGestureRecognizer*)tap{
    NSLog(@"單擊操作!");
    //獲取手勢監控的視圖對象
    UIImageView * imageView = (UIImageView*)tap.view;
    //開始動畫過程
    [UIView beginAnimations:nil context:nil];
    //設置動畫過度時間
    [UIView setAnimationDuration:2];

    imageView.frame=CGRectMake(0, 0, 320, 568);

    //結束動畫過程
    [UIView commitAnimations];

}

//雙擊操作
-(void)tapTwoGes:(UITapGestureRecognizer*)tap{
    NSLog(@"雙擊操作");
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    _imageView.frame =CGRectMake(50, 80, 200, 300);
    [UIView commitAnimations];


}

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

@end
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved