iOS 六种手势
2015-10-29 09:04
525 查看
1.点击手势
-(void)testTap{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealTap:)];
[_imageView
addGestureRecognizer:tap];
}
-(void)dealTap:(UITapGestureRecognizer *)tap{
CGPoint point = [tap
locationInView:_imageView];
NSLog(@"x=%f y=%f",point.x,point.y);
}
2.拖动手势
-(void)testPan{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealPan:)];
[_imageView
addGestureRecognizer:pan];
}
-(void)dealPan:(UIPanGestureRecognizer *)pan{
if (pan.state ==
UIGestureRecognizerStateBegan) {
_startPoint =
_imageView.center;
}
// locationInView和translationInView的区别
// 1 translationInView是UIPanGestureRecognizer下面的一个属性
// locationInView则是UIGestureRecognizer下面的属性
// 2 translationInView 在指定的坐标系中移动
// locationInView 通常是指单点位置的手势 得到当前点击下在指定视图中位置的坐标
if (pan.state ==
UIGestureRecognizerStateChanged) {
CGPoint offset = [pan
translationInView:self.view];
_imageView.center =
CGPointMake(_startPoint.x+offset.x,
_startPoint.y+offset.y);
}
}
3.旋转手势
-(void)testRotation{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealRotation:)];
[_imageView
addGestureRecognizer:rotation];
}
-(void)dealRotation:(UIRotationGestureRecognizer *)rotation{
double r = rotation.rotation;
_imageView.transform =
CGAffineTransformMakeRotation(r);
}
4.缩放手势
-(void)testPinch{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealPinch:)];
[_imageView
addGestureRecognizer:pinch];
}
-(void)dealPinch:(UIPinchGestureRecognizer *)pinch{
double scale = pinch.scale;
_imageView.transform =
CGAffineTransformMakeScale(scale, scale);
}
5.滑动手势
-(void)testSwipe{
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealSwipe:)];
swipe.direction =
UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
[_imageView
addGestureRecognizer:swipe];
}
-(void)dealSwipe:(UISwipeGestureRecognizer *)swipe{
_index ++;
if (_index ==
_images.count) {
_index =
0;
}
_imageView.image = [UIImage
imageNamed:_images[_index]];
}
6.长按手势
-(void)testLongPress
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealLongPress:)];
[_imageView
addGestureRecognizer:longPress];
}
-(void)dealLongPress:(UILongPressGestureRecognizer *)longPress
{
NSLog(@"longPress");//这句话会被打印两边
//注意:
这个事件处理方法会执行两次
if (longPress.state ==
UIGestureRecognizerStateBegan)
{
UIAlertView *alertView = [[UIAlertView
alloc] init];
alertView.message =
@"真的要保存吗";
[alertView addButtonWithTitle:@"确定"];
[alertView show];
}
}
-(void)testTap{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealTap:)];
[_imageView
addGestureRecognizer:tap];
}
-(void)dealTap:(UITapGestureRecognizer *)tap{
CGPoint point = [tap
locationInView:_imageView];
NSLog(@"x=%f y=%f",point.x,point.y);
}
2.拖动手势
-(void)testPan{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealPan:)];
[_imageView
addGestureRecognizer:pan];
}
-(void)dealPan:(UIPanGestureRecognizer *)pan{
if (pan.state ==
UIGestureRecognizerStateBegan) {
_startPoint =
_imageView.center;
}
// locationInView和translationInView的区别
// 1 translationInView是UIPanGestureRecognizer下面的一个属性
// locationInView则是UIGestureRecognizer下面的属性
// 2 translationInView 在指定的坐标系中移动
// locationInView 通常是指单点位置的手势 得到当前点击下在指定视图中位置的坐标
if (pan.state ==
UIGestureRecognizerStateChanged) {
CGPoint offset = [pan
translationInView:self.view];
_imageView.center =
CGPointMake(_startPoint.x+offset.x,
_startPoint.y+offset.y);
}
}
3.旋转手势
-(void)testRotation{
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealRotation:)];
[_imageView
addGestureRecognizer:rotation];
}
-(void)dealRotation:(UIRotationGestureRecognizer *)rotation{
double r = rotation.rotation;
_imageView.transform =
CGAffineTransformMakeRotation(r);
}
4.缩放手势
-(void)testPinch{
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealPinch:)];
[_imageView
addGestureRecognizer:pinch];
}
-(void)dealPinch:(UIPinchGestureRecognizer *)pinch{
double scale = pinch.scale;
_imageView.transform =
CGAffineTransformMakeScale(scale, scale);
}
5.滑动手势
-(void)testSwipe{
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealSwipe:)];
swipe.direction =
UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight;
[_imageView
addGestureRecognizer:swipe];
}
-(void)dealSwipe:(UISwipeGestureRecognizer *)swipe{
_index ++;
if (_index ==
_images.count) {
_index =
0;
}
_imageView.image = [UIImage
imageNamed:_images[_index]];
}
6.长按手势
-(void)testLongPress
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer
alloc] initWithTarget:self
action:@selector(dealLongPress:)];
[_imageView
addGestureRecognizer:longPress];
}
-(void)dealLongPress:(UILongPressGestureRecognizer *)longPress
{
NSLog(@"longPress");//这句话会被打印两边
//注意:
这个事件处理方法会执行两次
if (longPress.state ==
UIGestureRecognizerStateBegan)
{
UIAlertView *alertView = [[UIAlertView
alloc] init];
alertView.message =
@"真的要保存吗";
[alertView addButtonWithTitle:@"确定"];
[alertView show];
}
}
相关文章推荐
- 源码推荐(10.29):cell下拉点击,高仿百度糯米iOS
- iOS设置LaunchScreen后真机启动画面为黑屏的解决办法
- mac下开发IOS代码管理
- iOS中 喷枪打字动画的实现
- iOS中 喷枪打字动画的实现
- iOS9新特性 window决定程序的状态栏管理问题
- iOS开发——修复bug系列一objc-class-ref in UserSettingController.o
- iOS9,导航控制器中的子控制器设置StatusBar状态失效的问题
- iOS之富文本
- iOS性能优化那些事
- IOS设计模式-抽象工厂
- iOS 反向传值的几种方式
- iOS 正向传值demo
- TableViewController for IOS8
- iOS中NSURLSession的使用
- IOS 多线程原理
- iOS本地化存储笔记
- iOS-字典转双模型的实现过程中需要关注的细节
- iOS-字典转双模型的实现过程中需要关注的细节
- iOS获取屏幕的属性