#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