您的位置:首页 > 移动开发 > IOS开发

ios简单手势操作2

2014-01-03 17:40 357 查看
iOS中简单的手势操作:长按、捏合、移动和旋转

新建一个single view工程

ViewController.h文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
UIImageView *_imgView;
float _rotation;
}
@end


ViewController.m文件

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

_imgView = [[UIImageView alloc] initWithFrame:CGRectMake(110, 160, 100, 150)];
_imgView.image = [UIImage imageNamed:@"10_0.jpg"];
[self.view addSubview:_imgView];
[_imgView release];

_imgView.userInteractionEnabled = YES;

//长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
//    longPress.minimumPressDuration = 0;//效果等于轻触
longPress.minimumPressDuration = 2;//最少按两秒才会触发事件
//    [_imgView addGestureRecognizer:longPress];
[longPress release];

//捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchPress:)];
[_imgView addGestureRecognizer:pinch];
[pinch release];

//移动
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
[_imgView addGestureRecognizer:pan];
[pan release];

//旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
[_imgView addGestureRecognizer:rotation];
[rotation release];
}

- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
_imgView.transform = CGAffineTransformMakeRotation(_rotation + rotation.rotation);
if (rotation.state == UIGestureRecognizerStateCancelled) {
_rotation = rotation.rotation+_rotation;
}
NSLog(@"%f", _rotation);
}

- (void)pan:(UIPanGestureRecognizer *)pan
{
CGPoint point = [pan translationInView:self.view];
_imgView.center = CGPointMake(_imgView.center.x+point.x, _imgView.center.y+point.y);
[pan setTranslation:CGPointZero inView:self.view];//重置参考位置
}

- (void)pinchPress:(UIPinchGestureRecognizer *)pinch
{
CGSize _imgSize = CGSizeMake(100, 150);
float scale = pinch.scale;
_imgView.bounds = CGRectMake(0, 0, _imgSize.width*scale, _imgSize.height*scale);
//判断手势状态
if (pinch.state == UIGestureRecognizerStateCancelled) {
_imgSize = _imgView.bounds.size;
}
if (_imgSize.height <= 75) {
_imgSize.height = 75;
_imgSize.width = 50;
}
}

- (void)longPress:(UILongPressGestureRecognizer *)lp
{
NSLog(@"长按");
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息