为 UIImageView 添加Tap手势

//
//  ZJD_TapImageView.h
//  V6
//
//  Created by aidong on 15/8/18.
//  Copyright (c) 2015年 aidong. All rights reserved.
//

/**
 * 为UIImageView添加tap手势。
 * 用法:
     ZJD_TapImageView *iv = [[ZJD_TapImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
     iv.image = [UIImage imageNamed:@"temp_背景"];
     iv.imageViewBlock = ^(void){
        // tap后触发的方法
        [UIAlertView simpleAlert:@"tap!"];
     };
     [self addSubview:iv];
 */
#import <UIKit/UIKit.h>

typedef void (^TapImageViewBlock)(void);

@interface ZJD_TapImageView : UIImageView

@property (nonatomic, copy) TapImageViewBlock imageViewBlock;

@end
//
//  ZJD_TapImageView.m
//  V6
//
//  Created by aidong on 15/8/18.
//  Copyright (c) 2015年 aidong. All rights reserved.
//

#import "ZJD_TapImageView.h"

@implementation ZJD_TapImageView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageViewAction)];
        [self addGestureRecognizer:tap];
    }
    return self;
}

- (void)tapImageViewAction{
    if (self.imageViewBlock) {
        self.imageViewBlock();
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 15:54:02

为 UIImageView 添加Tap手势的相关文章

防止TableView 上的tap手势隔断 cell的选择

遵循UIGestureRecognizerDelegate协议: 1.0添加手势 - (void)addTapGest {    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];    tap.delegate = self;    [self.tableView addGestureRecognizer:tap];} 2.拦截部分ta

TableView添加拖动手势冲突 不能上下滚动

#pragma mark - 添加拖动手势- (void)addPanGestureRecognizer{    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];    panGesture.delegate=self;    [self.tableView addGestureRecognizer:

UI基础--(4)UIView深入认识、UIImageView动画及手势

知识点: 1.UIView的简单动画 1.UIView层次关系 2.UIImageView的使用 3.UIView 停靠模式 ===================== UIView的简单动画    1.UIVew坐标系统 1)UIView相对于父视图的坐标系统    2.UVIew的frame,center,bounds关系 frame:  该view在父view坐标系统中的位置和大小.(参照点是,父亲的坐标系统) bounds: 该view在本地坐标系统中的位置和大小.(参照点是,本地坐标系

Tap手势

使UIView能够支持点击的手势,需要用下面的代码: UITapGestureRecognizer *t = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(singleTap:)]; t.delegate = self; UIImageView *subView = (UIImageView*)[self.view viewWithTag:1234]; [subView addGestureRecognize

iOS:UIImageView添加点击事件

UIImageView并不像UIButton那样点击鼠标就可以关联点击事件,也不像Android里有onClickListener,这个时候就需要借助UITapGestureRecognizer类,从类名上就可以看出,这个类就是用于处理tap(单击)事件的. 创建两个UIImageView对象,imageView1和imageView2 [imageView1 setUserInteractionEnabled:YES]; [imageView2 setUserInteractionEnable

IOS开发: 为UIImageView添加点击事件

转载于:http://www.pocketdigi.com/20140218/1276.html UIImageView并不像UIButton一样,点点鼠标就可以关联点击事件,也不像Android里有onClickListener,这里需要借助于UITapGestureRecognizer类.从类名上就可以看出,这个类就是用于处理tap(单击)事件的.bbc和voaspecial是UIImageView对象 [bbc setUserInteractionEnabled:YES]; [voaspe

view上添加点手势 button无法响应点击事件

在view 上添加手势 有的时候  会把Button的 点击事件盖掉,这个 时候 我们用UITapGestureRecognizer的代理方法 //手势的代理方法 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { // 过滤掉UIButton,也可以是其他类型 if ( [touch.view isKindOfClass:[UIButto

#iOS开发常用方法集锦#为UITableView添加UISwipeGestureRecognizer手势

? 本文永久地址为http://www.cnblogs.com/ChenYilong/p/4103039.html ,转载请注明出处. 印象笔记链接:https://app.yinxiang.com/shard/s22/sh/04150175-aac6-4981-b71d-d7246de3037b/a0f139b2619a4607 ? ? ? <UIGestureRecognizerDelegate> -(void)viewDidLoad { ? ? [superviewDidLoad]; ?

iOS中添加UITapGestureRecognizer手势识别后,UITableView的didSelectRowAtIndexPath失效

ViewDidLoad中注册手势的部分代码如下: [cpp] view plaincopy UITapGestureRecognizer *oneTap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)] autorelease]; oneTap.delegate = self; oneTap.numberOfTouchesRequired = 1; [self.view a