#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//UIImageView使用
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 200, 280, 300)];
[imageView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:imageView];
[imageView release];
// 利用图片产生一个UIImage对象
UIImage *image = [UIImage imageNamed:@"伊泽.jpg"];
// 把图片加载到相框(UIImageView)
imageView.image = image;
//手势识别器
//1.轻拍手势
//手势需要在定义是绑定一个触发方法。
// UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapaction:)];
// //轻拍设置
// //需要轻拍两次
// tap.numberOfTapsRequired = 2;
// //需要用几根手指
// tap.numberOfTouchesRequired = 2;
// //给view添加一个手势
// [imageView addGestureRecognizer:tap];
// [tap release];
//将UIImageView的用户交互打开,使它们能够响应轻拍
// UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
// //长按触发方法 所需要的时间
// longPress.minimumPressDuration = 3;
// longPress.allowableMovement = 100;
// UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
// //设置清扫方向
// swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
// // swipe.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;
// [imageView addGestureRecognizer:swipe];
//
// [imageView addGestureRecognizer:longPress];
// [longPress release];
//拖拽手势(pan)
// UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
// [imageView addGestureRecognizer:pan];
// [pan release];
//旋转手势
// UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
// [imageView addGestureRecognizer:rotation];
// [rotation release];
// UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[imageView setUserInteractionEnabled:YES];
// [imageView addGestureRecognizer:pinch];
// [pinch release];
//屏幕边缘的拖拽
UIScreenEdgePanGestureRecognizer *screenEngePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenPan:)];
//设置监测哪一边 屏幕边缘
screenEngePan.edges = UIRectEdgeLeft;
[imageView addGestureRecognizer:screenEngePan];
[screenEngePan release];
}
- (void)screenPan:(UIScreenEdgePanGestureRecognizer *)screenPan
{
NSLog(@"边缘拖拽");
}
//捏合的触发方法
- (void) pinchAction:(UIPinchGestureRecognizer *)pinch
{
NSLog(@"捏合");
//获取当前的View
UIImageView *imageView = (UIImageView *)pinch.view;
//在x轴y轴放大 缩小。
imageView.transform = CGAffineTransformScale(imageView.transform,pinch.scale , pinch.scale);
pinch.scale = 1;
}
// 旋转
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
NSLog(@"旋转");
UIImageView *imageView = (UIImageView *)rotation.view;
imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
rotation.rotation = 0;
}
//拖拽的触发方法
- (void)panAction:(UIPanGestureRecognizer *)pan
{
NSLog(@"拖拽");
//通过手势的view属性 获取当前手势添加到 view.
UIImageView *imageView = (UIImageView *)pan.view;
//获取到当前手指接触的点
CGPoint p = [pan translationInView:imageView];
//让view变形
imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);
//重置手势的属性
[pan setTranslation:CGPointZero inView:imageView];
}
//轻扫的触发方法
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"轻扫");
}
//轻拍的触发方法
- (void)tapaction:(UITapGestureRecognizer *)tap
{
NSLog(@"轻拍");
}
// 长按的触发方法
- (void)longPressAction:(UILongPressGestureRecognizer *)tap
{
if (tap.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按");
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
iOSUI视图控制下的各种触摸方法及实现
最新推荐文章于 2019-02-23 11:19:17 发布