自定义带点击效果的imageView,自己封装了一个创建imageView的方法,代码如下:
1、.h文件声明属性和+方法
#import <UIKit/UIKit.h>
#importtypedef void (^tapClick)();//手势点击
@interface ZJImageView : UIImageView
@property(nonatomic)int id;
@property (nonatomic,strong) UILabel * titleLabel;
@property (nonatomic,strong) UIImageView * smallView;
//手势方法
+(ZJImageView *)imageViewWithframe:(CGRect )frame andClickBlock:(tapClick)myblock;
@end
2、.m文件中实现+方法
#import "ZJImageView.h"
@interface ZJImageView ()
@property(nonatomic,copy)tapClick tempBlock;//过度Block
@end
@implementation ZJImageView
/**
* @author zhengju, 16-06-30 11:06:42
*
* @brief 手势点击方法
*
* @param frame 创建UIImageView对象的frame
* @param myblock Block
*
* @return 返回带点击效果的imageView
*/
+(ZJImageView *)imageViewWithframe:(CGRect )frame andClickBlock:(tapClick)myblock{
ZJImageView *imageView = [[ZJImageView alloc]initWithFrame:frame];
imageView.tempBlock = myblock;
//添加点击手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:imageView action:@selector(tapMethod:)];
[imageView addGestureRecognizer:tap];
imageView.userInteractionEnabled = YES;
//活动文字背景
imageView.smallView = [[UIImageView alloc]initWithFrame:CGRectMake(0, frame.size.height-20, frame.size.width, 20)];
imageView.smallView.image = SET_IMAGE(@"活动文字背景");
[imageView addSubview:imageView.smallView];
//文字显示
imageView.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, frame.size.height-20, frame.size.width, 20)];
imageView.titleLabel.text = @"请匹配图片.....";
[imageView addSubview:imageView.titleLabel];
return imageView;
}
/**
* @author zhengju, 16-06-30 11:06:53
*
* @brief 手势方法
*
* @param tap 手势
*/
-(void)tapMethod:(UITapGestureRecognizer *)tap{
ZJImageView *imageView=(ZJImageView *)tap.view;
if (imageView.tempBlock) {//判断是否实现Block
imageView.tempBlock();
}
}