你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS開發從入門到精通--UIGesture手勢擴展,平移,滑動,長按

iOS開發從入門到精通--UIGesture手勢擴展,平移,滑動,長按

編輯:IOS開發綜合

UIGesture手勢擴展,平移,滑動,長按
這裡寫圖片描述
這裡寫圖片描述

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

    UIImageView * iView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"17_8.png"]];

    iView.frame=CGRectMake(50, 50, 200, 300);

    iView.userInteractionEnabled=YES;

    //創建一個平移手勢
    //p1:事件函數處理對象
    //p2:事件函數
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAct:)];

    //將手勢添加到圖像視圖中
    [iView addGestureRecognizer:pan];

    //將移動事件手勢從圖像視圖中取消
    [iView removeGestureRecognizer:pan];

    [self.view addSubview:iView];


    //創建一個滑動手勢
    UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAct:)];

    //設定滑動手勢接收事件的類型
    //UISwipeGestureRecognizerDirectionUp    上
    //UISwipeGestureRecognizerDirectionDown  下
    //UISwipeGestureRecognizerDirectionLeft  左
    //UISwipeGestureRecognizerDirectionRight 右
    swipe.direction =UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;

    [iView addGestureRecognizer:swipe];

    //創建長按手勢
    UILongPressGestureRecognizer * longPress =[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAct:)];
    [iView addGestureRecognizer:longPress];

    //設置長按手勢的時間,默認是0.5秒的時間為長按手勢
    longPress.minimumPressDuration=3;

}
//長按函數
-(void)longPressAct:(UILongPressGestureRecognizer*)longpress{
    //手勢的狀態對象,到達規定時間,3秒鐘,觸發函數
    if(longpress.state==UIGestureRecognizerStateBegan){
         NSLog(@"狀態開始");
    }
    //當手指離開屏幕時,結束狀態
    else if(longpress.state==UIGestureRecognizerStateEnded){
        NSLog(@"結束狀態");

    }

}

//滑動函數
-(void)swipeAct:(UISwipeGestureRecognizer*)swipe{
     NSLog(@"swipe滑動");
}

//移動事件函數,只要手指坐標在屏幕上發生變化時,函數就會被調用
-(void)panAct:(UIPanGestureRecognizer*)pan{

    //獲取移動的坐標,現對於視圖的坐標系
    //參數:相對的視圖對象

//    CGPoint pt = [pan translationInView:self.view];
//    NSLog(@"pt.x=%.2f,pt.y=%.2f",pt.x,pt.y);

    //獲取移動時的相對速度
    CGPoint pv = [pan velocityInView:self.view];
    NSLog(@"pv.x=%.2f,pv.y=%.2f",pv.x,pv.y);


}

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

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