//这是图片的类
#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)];

 

相关文章:

  • 2021-08-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-12
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-26
  • 2022-02-12
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
相关资源
相似解决方案