现在iPhone5的尺寸已经是4英寸了 不排除接下来会出现4.5 5.0英寸的iPhone,但是众所周知iOS的返回键大多在左上角,随着屏幕尺寸的增大,要按到座上角的返回键就没那么容易啊,所以现在的应用大多都会采用手势,用手势来代替一些按键操作。
所以在这里就回忆一下几种常用的手势咯,碰巧在网上看到一篇好文章,所以就跟着写了个demo 分享给大家
//
// ViewController.h
// gestureDemo
//
// Created by kay_sprint on 13-4-5.
// Copyright (c) 2013年 cn.edu.scnu. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface ViewController : UIViewController <UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@end
//
// ViewController.m
// gestureDemo
//
// Created by kay_sprint on 13-4-5.
// Copyright (c) 2013年 cn.edu.scnu. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic) CGFloat lastScale;
@property (nonatomic) CGFloat lastRotate;
@property (nonatomic) CGFloat firstX;
@property (nonatomic) CGFloat firstY;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.imgView.userInteractionEnabled = YES;
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
panGestureRecognizer.delegate = self;
[self.imgView addGestureRecognizer:panGestureRecognizer];
_lastScale = 1.0;
UIPinchGestureRecognizer *pinchGestureRecognicer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
pinchGestureRecognicer.delegate = self;
[self.imgView addGestureRecognizer:pinchGestureRecognicer];
_lastRotate = 0.0;
UIRotationGestureRecognizer *rotateGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];
rotateGestureRecognizer.delegate = self;
[self.imgView addGestureRecognizer:rotateGestureRecognizer];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
tapGestureRecognizer.delegate = self;
[self.imgView addGestureRecognizer:tapGestureRecognizer];
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer
{
CGPoint translatedPoint = [recognizer translationInView:self.view];
if(recognizer.state == UIGestureRecognizerStateBegan) {
_firstX = [[recognizer view] center].x;
_firstY = [[recognizer view] center].y;
}
translatedPoint = CGPointMake(translatedPoint.x + _firstX, translatedPoint.y + _firstY);
[recognizer.view setCenter:translatedPoint];
if(recognizer.state == UIGestureRecognizerStateEnded) {
CGFloat finalX = translatedPoint.x + (.2 * [recognizer velocityInView:self.view].x);
CGFloat finalY = translatedPoint.y + (.2 * [recognizer velocityInView:self.view].y);
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
if(finalX < 0) {
finalX = 0;
} else if(finalX > 768) {
finalX = 768;
}
if(finalY < 0) {
finalY = 0;
} else if(finalY > 1024) {
finalY = 1024;
}
} else {
if(finalX < 0) {
finalX = 0;
} else if(finalX > 1024) {
finalX = 1024;
}
if(finalY < 0){
finalY = 0;
} else if(finalY > 768) {
finalY = 768;
}
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[recognizer.view setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
- (void)handlePinch:(UIPinchGestureRecognizer *)recognizer
{
if(recognizer.state == UIGestureRecognizerStateEnded) {
_lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (_lastScale - recognizer.scale); //计算两次的差值
CGAffineTransform currentTransform = recognizer.view.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[recognizer.view setTransform:newTransform];
_lastScale = recognizer.scale;
}
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer
{
if(recognizer.state == UIGestureRecognizerStateEnded) {
_lastRotate = 0.0;
return;
}
CGFloat rotate = 0.0 - (_lastRotate - recognizer.rotation);
CGAffineTransform currentTransform = recognizer.view.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, rotate);
[recognizer.view setTransform:newTransform];
_lastRotate = recognizer.rotation;
}
- (void)handleTap:(UITapGestureRecognizer *)recognizer
{
//cancel all the animation
[recognizer.view.layer removeAllAnimations];
}
//delegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]; //pan不能和其他手势一起触发
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
代码比较简单,这里就不多说了