可点击的uiimageview

实现这个有很多方法,来看看我的小把戏。嘿嘿。

代码:

ClickImageView.h

@interface ClickImageView : UIImageView

{

    id _target;

    SEL _selector;

}



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





@end

ClickImageView.m

#import "ClickImageView.h"



@implementation ClickImageView



- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

         self.userInteractionEnabled=YES;

    }

    return self;

}



-(id)initWithImage:(UIImage *)image

{

    self=[super initWithImage:image];

    if (self) {

        self.userInteractionEnabled=YES;

    }

    return self;

}



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

{

    _target=target;

    _selector=selector;

}



-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    if([_target respondsToSelector:_selector]){

        [_target performSelector:_selector withObject:self];

    }

}

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // Drawing code

}

*/



-(void)dealloc

{

    [super dealloc];

}



@end

使用:

    imageView = [[ClickImageView alloc] initWithImage:image];

    [imageView addTarget:self selector:@selector(pressOnImageView:)];

 

你可能感兴趣的:(imageview)