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);
}
创建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);
}
相关文章推荐
- iOS学习笔记——使用ChildViewController
- IOS 开发,调用打电话,发短信,打开网址
- iOS 阶段学习第十天笔记(字符串操作)
- 监控系统nagios+nconf+pnp4
- iOS交叉编译
- iOS开发--漫谈内存管理(一)
- iOS开发基础笔记-算法<三>
- iOS 阶段学习第十天笔记(结构体)
- ios8.3 越狱之后,没有ps,top等命令
- 解决iOS Xcode 模拟器键盘不弹出
- iOS 并行开发技术之 NSOperation && GCD
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- iOS日志获取
- iOS 单元测试之XCTest详解(一)
- 在iOS 中创建自定义Delegates properties with ARC错误的解决方法
- 苹果开发 笔记(42)leftBarButtonItem
- nagios 安装和配置(含有nrpe结束)所有 (两)
- nagios安装和配置(个人笔记)
- iOS 阶段学习第九天笔记(内存管理)
- iOS 集成环信, 扩展属性, chatcell label 可被点击