你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS UI05_手勢識別器

iOS UI05_手勢識別器

編輯:IOS開發綜合

手勢:點擊,長按,旋轉,捏合,拖拽,清掃,清掃的方向

//
//  MainViewController.m
//  UI05_手勢識別器
//
//  Created by dllo on 15/8/4.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import MainViewController.h

@interface MainViewController ()
@property(nonatomic,retain)UIImageView *imageView;
@property(nonatomic,retain)UIAlertView *alertView;
@end

@implementation MainViewController
-(void)dealloc
{
    [_imageView release];
    [_alertView release];
    [super dealloc];
}



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

    //UIImageView
    UIImage *image=[UIImage imageNamed:@footRight_03.jpg.jpg];
    self.imageView=[[UIImageView alloc] initWithImage:image];
    self.imageView.frame=CGRectMake(50, 100, 300, 400);
    [self.view addSubview:self.imageView];
    [_imageView release];

    //把圖片的用戶交互打開,默認是關閉的,此外還有一個控件是label
    self.imageView.userInteractionEnabled=YES;

    //手勢的使用
    //1.點擊
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    //設置點擊幾次才會觸發方法
    tap.numberOfTapsRequired=2;
    //設置幾根手指進行點擊
    tap.numberOfTouchesRequired=2;

    //將手勢添加到對應的圖片上
    [self.imageView addGestureRecognizer:tap];
    [tap release];
     //2.長按
    UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    //設置長按觸發的最短時間
    longPress.minimumPressDuration =2;
    //用戶手指在長按過程中允許移動的距離
    longPress.allowableMovement=200;
    //把手勢添加到圖片上
    [self.imageView addGestureRecognizer:longPress];
    [longPress release];

    //3.旋轉
    //創建一個旋轉的手勢
    UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    //把手勢放到對應的圖片上
    [self.imageView addGestureRecognizer:rotation];
    //釋放
    [rotation release];
    //4.捏合
    //創建
    UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pincheAction:)];
    //添加到圖片上
    [self.imageView addGestureRecognizer:pinch];
    //釋放
    [pinch release];

    //5.拖拽
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [self.imageView addGestureRecognizer:pan];
    [pan release];
    //6.清掃
    UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    [self.imageView addGestureRecognizer:swipe];
    [swipe release];
    //清掃的方向
    swipe.direction=UISwipeGestureRecognizerDirectionUp;
    //屏幕邊界手勢 iOS7.0之後出現的手勢
//    UIScreenEdgePanGestureRecognizer




}



#pragma mark 點擊方法
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@djkdajdo);
    self.imageView.image=[UIImage imageNamed:@angry_05.jpg];
}
#pragma mark 長按時響應方法
-(void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
    //長按的狀態
//    longPress.state
    //長按之後彈出一個UIAlerView
    if (!self.alertView) {
            self.alertView=[[UIAlertView alloc] initWithTitle:@404 message:@報錯 delegate:self cancelButtonTitle:@確認 otherButtonTitles:nil, nil];
        [self.alertView show];
        [_alertView release];
    }

}
#pragma mark 通過圖片的旋轉手勢,讓圖片發生旋轉
-(void)rotationAction:(UIRotationGestureRecognizer *)rotarion
{
    //可以通過手勢獲取手勢添加的視圖是哪一個
    UIImageView *imageView=(UIImageView *)rotarion.view;
    //進行旋轉的操作
    //通過視圖的transform屬性,讓視圖進行旋轉
    imageView.transform=CGAffineTransformRotate(imageView.transform,rotarion.rotation);
    rotarion.rotation=0;
}
#pragma mark 通過捏合手勢,縮放圖片
-(void)pincheAction:(UIPinchGestureRecognizer *)pinche
{
    //通過手勢找視圖
    UIImageView *imageView=(UIImageView *)pinche.view;
    //通過transform改變圖片的尺寸
    imageView.transform=CGAffineTransformScale(imageView.transform, pinche.scale, pinche.scale);
    pinche.scale=1;

}
#pragma mark 通過拖拽手勢,讓試圖隨著手勢移動而移動
-(void)panAction:(UIPanGestureRecognizer *)pan
{
    UIImageView *imageView=(UIImageView *)pan.view;
    //通過手勢獲得經過的點
    CGPoint p=[pan translationInView:imageView];
    //設置移動的位置
    imageView.transform=CGAffineTransformTranslate(imageView.transform, p.x, p.y);
    //為了防止手勢在操作的時候試圖消失
    [pan setTranslation:CGPointZero inView:imageView];
}
#pragma mark 清掃的對應方法
-(void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    if (swipe.direction == UISwipeGestureRecognizerDirectionUp ) {
        NSLog(@上);
    }

}




- (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

 

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