Reputation: 14123
I have an UIImageView
on my view. Certain parts of that image, I want to use as UIButtons
. But the problem is, Those image parts are not of in a regular shape. I haven't really tried anything. The immediate solution which came to my mind was to recognize small frames of image and put custom button over that frame. But since the image portion (button area) is not of regular shape, this concept failed.
How can I place buttons on that UIImageView
so that particular portion(not of regular shape) of image responds to some action.
Consider the following image :
I want to place one button at the center circle which is dark. I want to place buttons in between two perpendicular lines so that a quarter of button is clickable each with different action. So, in all I have 5 buttons.
Upvotes: 0
Views: 277
Reputation: 19869
I can think of 2 options:
first one
-(CGFloat)percentFromHeight:(CGFloat)percent{
return (CGRectGetHeight(self.bounds)/100)*percent;
}
-(CGFloat)percentFromWidth:(CGFloat)percent{
return (CGRectGetWidth(self.bounds)/100)*percent;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self];
//first check if the tup is in the center
if(currentTouchPosition.x > [view percentFromWidth:45] && currentTouchPosition.x < [view percentFromWidth:55] && currentTouchPosition.y > [view percentFromWidth:45] && currentTouchPosition.y < [view percentFromWidth:55]){
//the tup is in the center
//Do your thing
return;
}
//if this is not in the center, check other options
if(currentTouchPosition.x > [view percentFromWidth:50]){
if(currentTouchPosition.y > [view percentFromHeight:50]{
//this is the bottom left side
else{
//this is the top left side
}
}else if(currentTouchPosition.x < [view percentFromWidth:50]{
if(currentTouchPosition.y > [view percentFromHeight:50]{
//this is the bottom right side
else{
//this is the top right side
}
}
second -
subclass UIButton / UIImage as you prefer and use UIBezierPath to draw custom shapes in drawRect:
, then use [UIBezierPath containsPoint]
to check if the those is inside the path of the view/button.
Upvotes: 3
Reputation: 10251
My opinion is you should use touches method to find which portion is touched(using the image frame and touchable portions' frame) and make appropriate action than adding a button.
Upvotes: 0