本文主要是介绍UIImageView 7种手势基本介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//UIImageView的使用
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 280, 300)];
[imageView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:imageView];
[imageView release];
//利用图片产生一个UIImage对象
UIImage *image = [UIImage imageNamed:@"11.png"];
// UIImage *image1 = [UIImage imageWithContentsOfFile:@"/Users/dlios/Desktop/u=596837638,1893145150&fm=21&gp=0.jpg"];
//把这张图片加载到相框(UIImageView)
imageView.image = image;
// imageView.image = image1;
//手势识别器
//1.轻拍手势
//手势需要在定义是绑定一个触发方法(SEL)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
// 轻拍的设置
// 需要轻拍两次才能响应事件
tap.numberOfTapsRequired = 2;
// 手指的个数
tap.numberOfTouchesRequired = 2;
//给view添加一个手势
[imageView addGestureRecognizer:tap];
[tap release];
//2.长按手势(longPress)
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[imageView addGestureRecognizer:longPress];
[longPress release];
//长按 触发方法 所需要的事件
longPress.minimumPressDuration = 0.5;
// 长按时 允许用户移动手指的距离
longPress.allowableMovement = 100;
//3. 清扫手势(swipe)
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
//设置清扫的方向
swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;
[imageView addGestureRecognizer:swipe];
[swipe release];
//4.拖拽手势(pan)
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[imageView addGestureRecognizer:pan];
[pan release];
//5.旋转手势(ratation)
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
[imageView addGestureRecognizer:rotation];
[rotation release];
//6.捏合手势(pinch)
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinch];
[pinch release];
//7.屏幕边缘拖拽
UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenPan:)];
//设置屏幕边缘拖拽的方向
screenEdgePan.edges = UIRectEdgeLeft;
[imageView addGestureRecognizer:screenEdgePan];
[screenEdgePan release];
//将UIImageView的用户交互打开, 使他能响应轻拍
[imageView setUserInteractionEnabled:YES];
}
// 轻拍的触发方法
- (void)tapAction:(UITapGestureRecognizer *)tap
{
NSLog(@"轻拍");
}
// 长按的触发方法
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按");
}
}
//清扫的触发方式
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"清扫");
}
//拖拽的触发方法
- (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)rotationAction:(UIRotationGestureRecognizer *)rotation
{
NSLog(@"旋转");
//获取到当前手势添加到得view
UIImageView *imageView = (UIImageView *)rotation.view;
//让view旋转 利用旋转手势的旋转弧度
imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
rotation.rotation = 0;
}
//捏合的触发方法
- (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)screenPan:(UIScreenEdgePanGestureRecognizer *)screenPan
{
NSLog(@"屏幕边缘拖拽");
}
这篇关于UIImageView 7种手势基本介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!