检测点击UIImageView IOS

前端之家收集整理的这篇文章主要介绍了检测点击UIImageView IOS前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
难道我做错了什么?我无法检测到UI Imageview上的点按. UIImageView * myImage是在storyboard中创建的.代码文件在这里: https://drive.google.com/folderview?id=0B2jWw-2wZC52NDBFZ2lXUTUzQXM&usp=sharing

文件:ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong,nonatomic) IBOutlet UIImageView *myImage;

@end

文件:ViewController.m

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view,typically from a nib.

    UIGestureRecognizer *tapGestureRecognizer = [[UIGestureRecognizer alloc] initWithTarget:self action:@selector(tappedImage:)];
    [self.myImage addGestureRecognizer:tapGestureRecognizer];
    self.myImage.userInteractionEnabled = YES; // default is no for UIImageView

}

- (void)tappedImage:(UIGestureRecognizer *)gestureRecognizer {
    //UIImageView *myImage = (UIImageView *)gestureRecognizer.view;
    // do stuff;
    NSLog(@"it works");
}


@end

解决方法

我认为你应该使用类UITapGestureRecognizer作为:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected)];
singleTap.numberOfTapsrequired = 1;
preArrowImage.userInteractionEnabled = YES;
[preArrowImage addGestureRecognizer:singleTap];

-(void)tapDetected{
NSLog(@"single Tap on imageview");

}

猜你在找的iOS相关文章