为image添加点击事件,重写类

//这是图片的类
#import <Foundation/Foundation.h>


@interface TapImageView : UIImageView {

}

- (void)addTarget:(id)target withSelector:(SEL)selector;

@end

 

 

#import "TapImageView.h"


@implementation TapImageView

- (void)addTarget:(id)target withSelector:(SEL)selector
{
	UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] 
											 initWithTarget:target action:selector];
	[self addGestureRecognizer:tapRecognizer];
	[tapRecognizer release];
	
	self.userInteractionEnabled = YES;			
}

@end

 

 

引用这个子类的时候

imageView1 = [[TapImageView alloc] initWithFrame:CGRectMake(-100, 50, 100, 100)];
	imageView1.backgroundColor = [UIColor redColor];
	[self.view addSubview:imageView1];
	[imageView1 release];
	[imageView1 addTarget:self withSelector:@selector(changeColor1)]; //给图片加上触摸事件
	
	imageView2 = [[TapImageView alloc] initWithFrame:CGRectMake(-100, 200, 100, 100)];
	imageView2.backgroundColor = [UIColor redColor];
	[self.view addSubview:imageView2];
	[imageView2 release];
	[imageView2 addTarget:self withSelector:@selector(changeColor2)];

	imageView3 = [[TapImageView alloc] initWithFrame:CGRectMake(-100, 350, 100, 100)];
	imageView3.backgroundColor = [UIColor redColor];
	[self.view addSubview:imageView3];
	[imageView3 release];
	[imageView3 addTarget:self withSelector:@selector(changeColor3)];

 

posted @ 2012-10-15 09:56  不曾拥有  阅读(945)  评论(0编辑  收藏  举报