OC手勢

#import <UIKit/UIKit.h>

 

@interface AppDelegate : UIResponder <UIApplicationDelegate>

 

@property (strong, nonatomic) UIWindow *window;

 

 

@end

 
 

//

//  AppDelegate.m

//  UI04_手勢

//

//  Created by dllo on 16/1/8.

//  Copyright © 2016年 ZHAO. All rights reserved.

//

 

#import "AppDelegate.h"

#import "RootViewController.h"

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

- (void)dealloc

{

    [_window release];

    [super dealloc];

}

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    

     self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    [self.window makeKeyAndVisible];

    self.window.backgroundColor = [UIColor whiteColor];

    RootViewController *vc = [[RootViewController alloc] init];

    self.window.rootViewController = vc;

    

    

    [vc release];

    [self.window release];

    

    

    return YES;

}

 

- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

 

- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

 

- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

 

- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

 

- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

 

@end

 
 

//

//  CustomView.h

//  UI04_手勢

//

//  Created by dllo on 16/1/8.

//  Copyright © 2016年 ZHAO. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface CustomView : UIView

 

@end

 
 

//

//  CustomView.m

//  UI04_手勢

//

//  Created by dllo on 16/1/8.

//  Copyright © 2016年 ZHAO. All rights reserved.

//

 

#import "CustomView.h"

 

@implementation CustomView

 

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/

 

@end

 

 

 

 

//

//  RootViewController.h

//  UI04_手勢

//

//  Created by dllo on 16/1/8.

//  Copyright © 2016年 ZHAO. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface RootViewController : UIViewController

 

@end

 

//

//  RootViewController.m

//  UI04_手勢

//

//  Created by dllo on 16/1/8.

//  Copyright © 2016年 ZHAO. All rights reserved.

//

 

#import "RootViewController.h"

 

@interface RootViewController ()

 

@end

 

@implementation RootViewController

 

 

- (void)dealloc

{ [self.view release];

    [super dealloc];

}

- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];

    imageView.center = self.view.center;

    imageView.image = [UIImage imageNamed:@"3.jpg"];

    

    //開啓用戶交互

    imageView.userInteractionEnabled = YES;

    

    

    

    [self.view addSubview:imageView];

    

    // 1. 點擊、輕拍(tap)

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

    // 執行方法需要點擊的次數{

    tap.numberOfTapsRequired = 2;

    // 需要幾根手指

    tap.numberOfTouchesRequired = 1;

    // 將手勢付給視圖

    [imageView addGestureRecognizer:tap];

    

    [tap release];

 

    // 2. 長按(longPress)

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];

    // 判定長按手勢需要時間, 默認爲0.5秒

    longPress.minimumPressDuration = 2;

    // 在判定過程中允許 用戶手指移動的距離

    longPress.allowableMovement = 200;

    // 添加長按手勢

    [imageView addGestureRecognizer:longPress];

    

     [longPress release];

    

    // 3.捏合(pinch)

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];

    

    [imageView addGestureRecognizer:pinch];

    

    [pinch release];

    

    

 

    // 4.拖拽/平移(pan)

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];

    [imageView addGestureRecognizer:pan];

    

    [pan release];

    

    

    // 5.旋轉(rotation)

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];

    [imageView addGestureRecognizer:rotation];

    

    [rotation release];

    

   

    [imageView release];

    

    // Do any additional setup after loading the view.

}

#pragma mark - 輕拍事件

- (void)tapAction:(UITapGestureRecognizer *)tap {

    NSLog(@"輕拍、點擊手勢");

    NSLog(@"%@", tap.view);

    

    

    UIImageView *imageView = (UIImageView *)tap.view;

    [imageView setImage:[UIImage imageNamed:@"1.jpg"]];

}

#pragma mark - 長按事件

 

     - (void)longPressAction:(UILongPressGestureRecognizer *)longPress {

         UIImageView *imageView = (UIImageView *)longPress.view;

         [imageView setImage:[UIImage imageNamed:@"3.jpg"]];

     }

#pragma mark - 捏合手勢

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {

    

    UIImageView *imageView = (UIImageView *)pinch.view;

    imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);

    pinch.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)rotationAction:(UIRotationGestureRecognizer *)rotation{

    UIImageView *imageView = (UIImageView *)rotation.view;

    imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);

}

 

#pragma mark - 輕掃手勢

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {

    

    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {

        NSLog(@"123");

    }

    

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    self.view.backgroundColor = [UIColor whiteColor];

    

    

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

 
 

//

//  ViewController.h

//  UI04_手勢

//

//  Created by dllo on 16/1/8.

//  Copyright © 2016年 ZHAO. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface ViewController : UIViewController

 

 

@end

 

//

//  ViewController.m

//  UI04_手勢

//

//  Created by dllo on 16/1/8.

//  Copyright © 2016年 ZHAO. All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

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