ios简单手势操作2

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值