您的位置:首页 > 产品设计 > UI/UE

UI手势

2015-12-07 21:17 323 查看
#import "RootViewController.h"

@interface
RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {

[super
viewDidLoad];

// Do any additional setup after loading the view.

//自定义一张图片

UIImageView *picture = [[UIImageView
alloc]initWithFrame:CGRectMake(50,
100, 300,
300)];

picture.image = [UIImage
imageNamed:@"053e01451f7f39e5d4c53d20d8998672.jpg"];

picture.userInteractionEnabled =
YES;

[self.view
addSubview:picture];

[picture
release];

/*******************
手势 ******************/

//轻拍(Tap)

/*

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

//设置轻拍次数 (默认1)

tap.numberOfTapsRequired = 2;

//设置轻拍的手指个数 (默认1)

tap.numberOfTouchesRequired = 2;

[picture addGestureRecognizer:tap];

[tap release];

*/

/*

//轻扫(Swipe)

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];

//设置轻扫方向

#warning 注意:
水平或者垂直方向只能选择一个方向.

swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight ;

//设置轻扫手指个数

swipe.numberOfTouchesRequired = 2;

[self.view addGestureRecognizer:swipe];

[swipe release];

*/

/*

//捏合(Pich)

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

[self.view addGestureRecognizer:pinch];

[pinch release];

*/

/*

//旋转(Rotation)

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

[picture addGestureRecognizer:rotation];

[rotation release];

*/

/*

//平移(Pan)

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

[picture addGestureRecognizer:pan];

[pan release];

*/

//长按 (LongPress)

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

//设置长按时间

longPress.minimumPressDuration =
2;

[picture
addGestureRecognizer:longPress];

[longPress
release];

}

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

NSLog(@"被轻拍");
}

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

NSLog(@"轻扫");
}

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

//放大缩小图片

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

// pic.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);

//参数说明: 1.当前视图的transform 2.width的缩放比例 3.height的缩放比例
pic.transform =
CGAffineTransformScale(pic.transform, pinch.scale, pinch.scale);

//缩放结束之后,
将缩放比例置为1.0
pinch.scale =
1.0;

NSLog(@"捏合");
}

-(void)rotationAction:(UIRotationGestureRecognizer *)rotation
{

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

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

//将当前旋转位置置为零度
rotation.rotation =
0.0;

NSLog(@"旋转");
}

-(void)panAction:(UIPanGestureRecognizer *)pan
{

UIImageView *pic = (UIImageView *)pan.view;

CGPoint p = [pan translationInView:pic];

pic.transform =
CGAffineTransformTranslate(pic.transform, p.x, p.y);

// [pan setTranslation:CGPointMake(0, 0) inView:pic];

//将当前位置,
设置成起始位置

[pan setTranslation:CGPointZero
inView:pic];

NSLog(@"平移");
}

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

NSLog(@"长按");

//长按时刻

if (longPress.state ==
UIGestureRecognizerStateBegan) {

[longPress.view
removeFromSuperview];

}

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