手勢操作

import "AppDelegate.h"

@interface AppDelegate ()<UIGestureRecognizerDelegate>

@property (nonatomic ,retain)UIImageView *imageView;

@end

@implementation AppDelegate
// 自定義按鈕的回調
-(void)customBtn:(UIButton*)sender{
NSLog(@"************");
}

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [self.window setRootViewController:[[UIViewController alloc]init]];

    // 自定義按鈕
    CustomButton *customBtn = [[CustomButton alloc]initWithFrame:CGRectMake(50, 30, 80, 50)];
    [customBtn setTitle:@"我是假冒的"];
    [customBtn addTarget:self action:@selector(customBtn:) forControlEvents:customButtonTouchUpInside];
    [self.window addSubview:customBtn];

    // 初始化一個視圖(響應者)來承載手勢
    UIView *gestureView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    // 當前視圖放置在屏幕中
    gestureView.center = self.window.center;
    gestureView.backgroundColor = [UIColor redColor];
    [self.window addSubview:gestureView];

    // 初始化textField(回收鍵盤所用)
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(150, 150, 100, 50)];
    textField.placeholder = @"請輸入。。。。";
    textField.tag = 1000;
    [self.window addSubview:textField];

      //  初始化UIImageView
    

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(150, 200, 150, 200)];
    imageView.tag = 3000;

    // imageView動圖
    NSMutableArray *mArray = [NSMutableArray array];
    for (int i = 1 ; i < 12 ; i++) {
    [mArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.tiff",i]]];
    }
    imageView.animationImages = mArray;
    // 設置動畫執行的時長(單位為秒)
    imageView.animationDuration = 1.5;
    // 開始動畫
    [imageView startAnimating];
    // 動畫結束
    [imageView stopAnimating];

    imageView.image = [UIImage imageNamed:@"11.png"];
    // UIImageView 的用戶交互默認是關閉的,要想使他處理觸摸事件,我們需要手動打開它
    imageView.userInteractionEnabled = YES;
    [self.window addSubview:imageView];

    // 創建開啟、結束動畫的按鈕
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(150, 0, 80, 50);
    [btn setTitle:@"開啟/停止" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn];

    // 輕拍手勢
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];

    // 設置觸控對象(觸控點個數)
    [tapGR setNumberOfTouchesRequired:1];
    // 設置輕拍的次數(點擊次數)
    [tapGR setNumberOfTapsRequired:2];
    // 給創建好的視圖添加手勢(一個視圖可以添加多個手勢,但是一個手勢只能添加到一個視圖上面)
    // [gestureView addGestureRecognizer:tapGR];
    // 點擊屏幕空白處回收鍵盤(取消textField的第一響應者)
    // [self.window addGestureRecognizer:tapGR]; // 回收鍵盤所用
    // 給圖片添加手勢
    [imageView addGestureRecognizer:tapGR];

    // 捏合手勢
    UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
    [imageView addGestureRecognizer:pinchGR];
    pinchGR.delegate = self;

    // 旋轉手勢
    UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
    [imageView addGestureRecognizer:rotationGR];

      //  平移手勢
    

// UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
// [imageView addGestureRecognizer:panGR];

//  屏幕邊緣輕掃手勢
UIScreenEdgePanGestureRecognizer *screenEdgePanGR = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenEdgePanAction:)];
[self.window addGestureRecognizer:screenEdgePanGR];

//  長按手勢
UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[self.window addGestureRecognizer:longPressGR];

//  輕掃手勢
UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
[self.window addGestureRecognizer:swipeGR];
return YES;

};

pragma mark -- 手勢的代理方法

// 使得多個手勢可以同時相應
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
// 返回值為YES時,當執行一個手勢的操作的時候,也可以執行其他手勢的操作
return YES;
}

// 輕拍手勢的回調方法
-(void)tapAction:(UITapGestureRecognizer*)sender{
// 可以根據手勢得到它當前所作用的視圖
// UIImageView imageView = (UIImageView)sender.view;
// imageView.frame = CGRectMake(50, 50, 50, 50);

NSLog(@"我輕拍了gestureView");

// // 得到textField(注意類型強轉)
// UITextField tagTextField = (UITextField)[self.window viewWithTag:1000];
// // 回收鍵盤 (取消第一響應者)
// [tagTextField resignFirstResponder];
}

// 捏合手勢的回調方法
-(void)pinchAction:(UIPinchGestureRecognizer*)sender{

//  通過捏合手勢得到縮放比例
float scale = sender.scale;
NSLog(@"%.1f",scale);
    
//  得到該手勢作用的視圖
UIView *view = sender.view;
//  2D仿射變換函數中 的縮放函數來實現視圖的放大縮小
//  是在原有基礎上改變當前視圖
// <#CGAffineTransform t#>參數:現有視圖的transform值
//  CGFloat sx 參數:x軸上的縮放比例
//  <#CGFloat sy#>參數:y軸上的縮放比例

// view.transform = CGAffineTransformMakeScale(0.5, 0.5); // 在視圖最初(初始狀態)的transform狀態上改變,不管執行多少次,都是以最初的transform狀態為基礎來改變的
view.transform = CGAffineTransformScale(view.transform, scale, scale);
// 每次捏合動作完畢之后,上次捏合值復原,使它每次都是從100%開始縮放
sender.scale = 1;
NSLog(@"捏合手勢");
}

// 旋轉手勢的回調方法
-(void)rotationAction:(UIRotationGestureRecognizer*)sender{
// 通過該手勢得到旋轉角度
float rotation = sender.rotation;

//  得到該手勢作用的視圖
UIView *view = sender.view;
//  通過2D仿射變換函數中的旋轉函數來使得當前視圖旋轉
//  <#CGAffineTransform t#>參數:現有視圖的transform值(矩陣)
//  <#CGFloat angle#>參數:旋轉角度
view.transform = CGAffineTransformRotate(view.transform, rotation);
    //  旋轉之后復原
sender.rotation = 0;

}

// 平移手勢的回調方法
-(void)panAction:(UIPanGestureRecognizer*)sender{
// 得到該手勢作用的視圖
UIView *view = sender.view;
// 得到我們在視圖上移動的偏移量
CGPoint currentPoint = [sender translationInView:view.superview];
// 通過2D仿射變換函數中與位移有關的函數實現視圖位置變化
view.transform = CGAffineTransformTranslate(view.transform, currentPoint.x , currentPoint.y);
// 復原
[sender setTranslation:CGPointZero inView:view.superview];
NSLog(@"平移手勢");
}

// 屏幕邊緣輕掃手勢回調方法
-(void)screenEdgePanAction:(UIScreenEdgePanGestureRecognizer*)sender{
NSLog(@"屏幕邊緣輕掃手勢");
}

// 長按手勢的回調方法
-(void)longPressAction:(UILongPressGestureRecognizer*)sender{
// 設置當前長按最小的時長
sender.minimumPressDuration = 1.5;
// 設置允許移動的范圍
sender.allowableMovement = 2;
NSLog(@"長按手勢");
}

// 輕掃手勢
-(void)swipeAction:(UISwipeGestureRecognizer*)sender{
sender.numberOfTouchesRequired =1;
sender.direction = UISwipeGestureRecognizerDirectionLeft;
NSLog(@"向左輕掃手勢");
}

// 動畫開關按鈕的回調方法
-(void)buttonAction:(UIButton*)sender{
UIImageView imageView = (UIImageView)[self.window viewWithTag:3000];
if (imageView.isAnimating) {
[imageView stopAnimating];
[imageView setImage:[UIImage imageNamed:@"11.png"]];
}else{
imageView.image = nil;
[imageView startAnimating];
}
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容