`

UIImageView响应点击事件

 
阅读更多

 

  1. UIImageView *imageView = [self getYourImageView];
    imageView.image = [self getYourImage];
    imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(yourHandlingCode:)];
    [imageView addGestureRecognizer:singleTap];
    [singleTap release];
     UITapGestureRecognizer 的用法
//单指单击
UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
 action:@selector(handleSingleFingerEvent:)];
 singleFingerOne.numberOfTouchesRequired = 1; //手指数
 singleFingerOne.numberOfTapsRequired = 1; //tap次数
 singleFingerOne.delegate = self;

 //单指双击
 UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
 action:@selector(handleSingleFingerEvent:)];
 singleFingerTwo.numberOfTouchesRequired = 1;
 singleFingerTwo.numberOfTapsRequired = 2;
 singleFingerTwo.delegate = self;
 //双指单击
UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleDoubleFingerEvent:)];
 doubleFingerOne.numberOfTouchesRequired = 2;
 doubleFingerOne.numberOfTapsRequired = 1;
doubleFingerOne.delegate = self;
UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
                     action:@selector(handleDoubleFingerEvent:)];
 doubleFingerTwo.numberOfTouchesRequired = 2;
 doubleFingerTwo.numberOfTapsRequired = 2;
 doubleFingerTwo.delegate = self;
//如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理
[singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];
 //同理双指亦是如此
 [doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];
 [self.view addGestureRecognizer:singleFingerOne];
[self.view addGestureRecognizer:singleFingerTwo];
[self.view addGestureRecognizer:doubleFingerOne];
 [self.view addGestureRecognizer:doubleFingerTwo];
 [singleFingerOne release];
 [singleFingerTwo release];
[doubleFingerOne release];
 [doubleFingerTwo release];
处理事件的方法,代码:
//处理单指事件
 - (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender
{
if (sender.numberOfTapsRequired == 1) {
 //单指单击
 NSLog(@"单指单击");
}else if(sender.numberOfTapsRequired == 2){
 //单指双击
NSLog(@"单指双击");
}
 }
 //处理双指事件
 - (void)handleDoubleFingerEvent:(UITapGestureRecognizer *)sender
{
 if (sender.numberOfTapsRequired == 1) {
 //双指单击
NSLog(@"双指单击");
}else if(sender.numberOfTapsRequired == 2){
//双指双击
 NSLog(@"双指双击");
}
}
  下面这个比较全一下:
UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的手势。UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有下面一些子类用于处理具体的手势: 

    1、拍击UITapGestureRecognizer (任意次数的拍击)  

    2、向里或向外捏UIPinchGestureRecognizer (用于缩放)  

    3、摇动或者拖拽UIPanGestureRecognizer  

    4、擦碰UISwipeGestureRecognizer (以任意方向)  

    5、旋转UIRotationGestureRecognizer (手指朝相反方向移动)  

    6、长按UILongPressGestureRecognizer 

对于不同类型的手势识别器,具有不同的配置属性。比如UITapGestureRecognizer,可以配置拍击次数。界面接收到手势之后,可以发送一个消息,用于处理响应手势动作后的任务。当然,不同的手势识别器,发送的消息方法也会有所不同。下面列举几个具体示例代码: 

 

一个手指,拍击两次手势

// 创建一个手势识别器

UITapGestureRecognizer *oneFingerTwoTaps =

  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];

// Set required taps and number of touches

[oneFingerTwoTaps setNumberOfTapsRequired:2];

[oneFingerTwoTaps setNumberOfTouchesRequired:1];

// Add the gesture to the view

[[self view] addGestureRecognizer:oneFingerTwoTaps];

消息方法oneFingerTwoTaps

- (void)oneFingerTwoTaps

{

  NSLog(@"Action: One finger, two taps");

}

两个手指,拍击两次手势

    UITapGestureRecognizer *twoFingersTwoTaps =

      [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease];

    [twoFingersTwoTaps setNumberOfTapsRequired:2];

    [twoFingersTwoTaps setNumberOfTouchesRequired:2];

    [[self view] addGestureRecognizer:twoFingersTwoTaps];

消息方法twoFingersTwoTaps

    - (void)twoFingersTwoTaps {

      NSLog(@"Action: Two fingers, two taps");

    }

 

一个手指向上、向下擦碰手势

// 向上擦碰

    UISwipeGestureRecognizer *oneFingerSwipeUp =

      [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];

    [oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];

    [[self view] addGestureRecognizer:oneFingerSwipeUp];

     

    - (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer

    {

      CGPoint point = [recognizer locationInView:[self view]];

      NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);

    }

    // 向下擦碰

    UISwipeGestureRecognizer *oneFingerSwipeDown =

      [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease];

    [oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];

    [[self view] addGestureRecognizer:oneFingerSwipeDown];

     

    - (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer

    {

      CGPoint point = [recognizer locationInView:[self view]];

      NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);

    }

 旋转手势

UIRotationGestureRecognizer *twoFingersRotate =

  [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];

[[self view] addGestureRecognizer:twoFingersRotate];

 

- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer

{

  // Convert the radian value to show the degree of rotation

  NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));

}

向里或向外捏的手势

UIPinchGestureRecognizer *twoFingerPinch =

  [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];

[[self view] addGestureRecognizer:twoFingerPinch];

 

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer

{

  NSLog(@"Pinch scale: %f", recognizer.scale);

}
 




分享到:
评论

相关推荐

    IOS 中UIImageView响应点击事件

    本篇文章将详细介绍如何在iOS中让UIImageView响应点击事件。 首先,为了让UIImageView能够接收触摸事件,我们需要将其`userInteractionEnabled`属性设置为`YES`。这个属性默认为`NO`,因此我们需要手动开启它: ``...

    iOS图片绘制(自定义UIImageView)

    通过以上步骤,我们已经成功地创建了一个自定义的`UIImageView`,它不仅能够展示图片,还具有标题,并对点击事件作出响应。这种自定义视图可以灵活应用于多种场景,例如卡片式布局、相册视图等,增加了应用的交互性...

    imageView点击穿透事件示例

    为了实现点击穿透,我们需要让`UIImageView`在接收到点击事件后不阻止事件继续向子视图传递。 一种实现方法是为`UIImageView`创建一个自定义分类,然后重写`hitTest:withEvent:`方法。`hitTest:withEvent:`是用于...

    实现图片浏览和切换且响应点击事件

    在iOS开发中,实现图片浏览和切换以及响应点击事件是一项常见的功能需求,尤其在制作App启动页、产品展示或者用户引导界面时。本项目提供的源码着重于这一部分的实现,下面将详细讲解相关的知识点。 首先,对于...

    UIImageView的手势

    以下是如何为UIImageView添加点击事件的示例: ```swift let tapGesture = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped(_:))) imageView.addGestureRecognizer(tapGesture) @objc ...

    iPhone之UIImageView入门

    4. **交互性**: 默认情况下,UIImageView是不响应用户交互的。如果你想让用户可以点击图片,你需要设置`userInteractionEnabled`为`true`,然后添加手势识别器,如TapGestureRecognizer。 5. **动画效果**: ...

    iOS UIButton 点击无响应的解决办法

    默认情况下,UIImageView不允许接收用户交互,因此如果直接在其上添加按钮,按钮将无法响应点击。解决这个问题的简单方法是设置父视图的`userInteractionEnabled`属性为`YES`,确保其能够接收和传递触摸事件。 其次...

    iOS_UIImageView and 手势

    此外,`UIImageView`默认不响应用户交互,若想添加手势,需设置`imageView.isUserInteractionEnabled = true`。为了保持良好的用户体验,还需要合理设定手势的灵敏度和范围,以及考虑手势之间的协调。 总结来说,`...

    IOS 滚动视图加点击事件的封装1

    在本案例中,"IOS 滚动视图加点击事件的封装1" 提到的功能是将滚动视图与点击事件相结合,以一种简洁的方式处理用户的交互。 首先,我们需要理解如何在UIScrollView中添加点击事件。通常情况下,滚动视图本身并不...

    Swift 2.1 为 UIView 添加点击事件和点击效果

    总结来说,通过上述方法,我们可以让`UIView`不仅能够响应点击事件,还能提供视觉反馈,增强用户体验。这在没有使用`UIButton`的场景下尤其有用,比如自定义复杂的布局或者需要定制化交互效果时。在实际开发中,可以...

    UITableViewCell带图片点击

    此外,如果希望整个单元格都能响应点击,还需要在`UITableViewDelegate`中实现`tableView(_:didSelectRowAt:)`方法: ```swift func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { ...

    点击旋转图片再点击停止旋转

    5. **用户交互**:为了响应用户的点击事件,我们需要为UIImageView添加手势识别器(如UIButton或UITapGestureRecognizer)。当用户点击图片时,触发相应的回调函数,从而开始或结束旋转动画。 6. **Block和闭包**:...

    ios-图片切换,点击缩放.zip

    总结来说,"ios-图片切换,点击缩放.zip"项目的核心技术包括:使用UIScrollView实现图片切换,UIImageView的全屏显示,添加UITapGestureRecognizer进行点击事件处理,以及利用UIScrollView的缩放功能来实现图片的...

    IOS应用源码之点击放大图片 .rar

    当用户点击图片时,通常会涉及到`UIImageView`的交互事件处理。 2. **手势识别器(GestureRecognizer)**: 为了检测用户的点击行为,开发者通常会添加`UITapGestureRecognizer`到`UIImageView`上。手势识别器允许应用...

    IOS应用源码之ios点击小图切换大图 .rar

    在这个项目中,我们会用到UIImageView来显示图片,以及UIButton来响应用户的点击事件。 2. **UIImageView**:UIImageView是用于展示静态图像的UI控件。在实现小图切换大图的功能时,我们通常会设置两个UIImageView...

    点击放大图片.zipIOS应用例子源码下载

    在这个例子中,我们可能会用到UIImageView来展示图片,并可能通过手势识别(如UITapGestureRecognizer)来监听用户的点击事件。 2. **UIImageView**:UIImageView是显示静态图像的视图,可以用来展示用户点击放大...

    ios 网页中图片点击放大效果demo

    这个“ios 网页中图片点击放大效果demo”提供了在iPhone 6.1上测试成功的实例,旨在展示如何在UIWebView中处理图片点击事件并实现放大效果。以下是对这个知识点的详细讲解: 1. **UIWebView**: UIWebView是Apple ...

    [iOS开发教程-3]Create a Detail View Page using UIImageView, UITextView and UILabel

    此外,通过IBAction可以响应用户的交互事件,如点击按钮跳转到其他页面。 在"Primer"阶段,这个教程可能还会涵盖如何在不同屏幕尺寸下适配这些组件,这通常涉及到Auto Layout和Size Classes的概念。Auto Layout能够...

Global site tag (gtag.js) - Google Analytics