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

iOS七大手势之(平移、捏合、轻扫、屏幕边缘轻扫)手势识别器方法

2015-06-28 21:10 1071 查看
//
创建UIImageView对象
UIImage *image = [UIImage imageNamed:@"5.jpg"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = CGRectMake(30, 20, 500, 500);
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
[imageView release];


//
给imageView添加手势识别器(轻拍)
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
[imageView addGestureRecognizer:tapGestureRecognizer];
[tapGestureRecognizer release];


//
给imageView添加手势识别器(旋转)
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(romation:)];
[imageView addGestureRecognizer:rotationGestureRecognizer];
[rotationGestureRecognizer release];


//
给imageView添加手势识别器(捏合)
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
[imageView addGestureRecognizer:pinchGestureRecognizer];
[pinchGestureRecognizer release];


//
给imageView添加手势识别器(长按)
UILongPressGestureRecognizer *longGestureRecongnizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
[imageView addGestureRecognizer:longGestureRecongnizer];
[longGestureRecongnizer release];


//
给imageView添加手势识别器(平移)
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureRecognizer:)];
[imageView addGestureRecognizer:panGestureRecognizer];
[panGestureRecognizer release];


//
给imageView添加手势识别器(轻扫)
UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureRecognizer:)];
/*
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
*/
//
轻扫的方向
swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[imageView addGestureRecognizer:swipeGestureRecognizer];
[swipeGestureRecognizer release];


//
给imageView添加手势识别器(屏幕边缘轻扫)
UIScreenEdgePanGestureRecognizer *screenEdgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self
action:@selector(screenEdgePanGestureRecognizer:)];
/*
UIRectEdgeNone = 0,
UIRectEdgeTop = 1 << 0,
UIRectEdgeLeft = 1 << 1,
UIRectEdgeBottom = 1 << 2,
UIRectEdgeRight = 1 << 3,
UIRectEdgeAll = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight
*/
screenEdgePanGestureRecognizer.edges = UIRectEdgeTop;
[imageView addGestureRecognizer:screenEdgePanGestureRecognizer];
[screenEdgePanGestureRecognizer release];

// 轻拍手势的方法
- (void)tap:(UIGestureRecognizer *)tapGestureRecognizer
{
UIView *view = tapGestureRecognizer.view;
//
修改视图的大小
view.bounds = CGRectMake(0, 0, 100, 100);
NSLog(@"a");
}

// 旋转手势的方法
- (void)romation:(UIRotationGestureRecognizer *)rotationGestureRecognizer
{
UIView *view = rotationGestureRecognizer.view;


// //
顺时针旋转PI/4
// view.transform = CGAffineTransformMakeRotation(M_PI_4);


// //
逆时针旋转PI/4
// view.transform = CGAffineTransformRotate(view.transform, -M_PI_4);


//
可以两个方向旋转
view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);

}

// 捏合手势的方法
- (void)pinch:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{


UIView *view = pinchGestureRecognizer.view;

//
让视图在原来的基础上放大1.5倍
view.transform = CGAffineTransformScale(view.transform, 1.5, 1.5);
// //
让视图在原来的基础上缩小0.5倍
// view.transform = CGAffineTransformScale(view.transform, 0.5, 0.5);
}

// 长按手势的方法
- (void)longPress:(UILongPressGestureRecognizer *)longPressGestureRecongizer
{


NSLog(@"长按");
}

// 平移手势的方法
- (void)panGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer
{
UIView *view = panGestureRecognizer.view;


CGPoint delPoint = [panGestureRecognizer translationInView:view];



//
视图是在上次的基础上平移
view.transform = CGAffineTransformMakeTranslation(delPoint.x,delPoint.y);

//
每次平移后,x与y轴的平移是不是归零
[panGestureRecognizer setTranslation:(CGPointZero) inView:view];
NSLog(@"平移");
}

// 轻扫手势的方法
- (void)swipeGestureRecognizer:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
UIImageView *view = (UIImageView *)swipeGestureRecognizer.view;
if (swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionRight) {
view.image = [UIImage imageNamed:@"9.jpg"];
}
}

// 屏幕边缘轻扫手势的方法
- (void)screenEdgePanGestureRecognizer:(UIScreenEdgePanGestureRecognizer *)screenEdgePanGestureRecognizer
{
UIImageView *view = (UIImageView *)screenEdgePanGestureRecognizer.view;
view.bounds = CGRectMake(0, 0, 200, 100);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: