简体   繁体   English

UIImageView上的多个按钮

[英]Multiple buttons on UIImageView

I have an UIImageView on my view. 我的视图上有一个UIImageView Certain parts of that image, I want to use as UIButtons . 该图像的某些部分,我想用作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. 如何在该UIImageView上放置按钮,以使图像的特定部分(非常规形状)响应某些操作。

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. 因此,我总共有5个按钮。

I can think of 2 options: 我可以想到2种选择:

  1. 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 } } 
  2. 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. 根据需要选择子类UIButton / UIImage并使用UIBezierPath在drawRect:绘制自定义形状,然后使用[UIBezierPath containsPoint]检查这些形状是否在视图/按钮的路径内。

阅读本主题 -甚至是同一张图片。

我的意见是,您应该使用touches方法来找到要触摸的部分(使用图像框和可触摸部分的框),并采取适当的措施,而不是添加按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM