Sunday, 25 December 2011

Detact tap on UIImage in Iphone

Create class name thumbImageView.
and put this code in thumbImageView.h

@interface thumbImageView : UIImageView {

id <thumbImageViewDelegate> delegate;
CGPoint touchLocation;
}
@property (nonatomic, assign) id <thumbImageViewDelegate> delegate;
@property (nonatomic, assign) CGPoint touchLocation;
@end
@protocol thumbImageViewDelegate <NSObject>
@optional
-(void)thumbImageViewWasTapped:(thumbImageView *)tiv;
@end

And now in  thumbImageView.m put this code

#import "thumbImageView.h"
@implementation thumbImageView
@synthesize delegate;
@synthesize touchLocation;
- (id)initWithImage:(UIImage *)image {
    self = [super initWithImage:image];
    if (self) {
        [self setUserInteractionEnabled:YES];
        [self setExclusiveTouch:YES];  // block other touches while dragging a thumb view
    }
    return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    touchLocation = [[touches anyObject] locationInView:self];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([[touches anyObject] tapCount] == 1) {
        if ([delegate respondsToSelector:@selector(thumbImageViewWasTapped:)])
            [delegate thumbImageViewWasTapped:self];
    }
NSArray *allTouches = [touches allObjects]; 
    UITouch *touch = [touches anyObject];
    
    int count = [allTouches count];
    
    if (count == 1
    {
        if ([touch tapCount] < 2
        {
        }
    }
}

After implement these two class  now you can use these class.
Import Delegete like

@interface Your Class : UIViewController
<thumbImageViewDelegate>
in your Class.m
create image like->
     thumbImageView *imgView = [[thumbImageView alloc] initWithFrame:CGRectMake(x, y, H,W)];
and paste this function in your class
-(void)thumbImageViewWasTapped:(thumbImageView *)tiv;
{
//Here you can detect the tap on tag
//tiv.tag
}




No comments:

Post a Comment