2017-04-21 75 views
0

我在这里遇到问题。我希望按钮能够完美地适应背景图像的尺寸。缩放按钮以适应背景图像

例如:

Image Example

正如你可以看到,围绕“请求计划”钻石箱重叠或越过“查看计划”的钻石,并在运行时,当我点击“查看计划”钻石由于它重叠而实际点击“请求计划”钻石。有没有办法解决这个问题?

+0

检查,您可以创建一个正常的按钮,然后将其旋转。在这种情况下,你的背景图像需要被“旋转”版本 – jokeman

+0

@jokeman我无法找到一个旋转选项,我该怎么做 – user3694341

+1

尝试'self.myButton.transform = self.myButton.transform.rotated(by:CGFloat( M_PI_4))'当初始化你的按钮 – jokeman

回答

0

您可以创建CustomButton类扩展的UIButton然后覆盖pointInside

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool { 
    *we need check at this point inside or outside* 
    return self.alphaAtPoint(point) >= 127 
} 

,我们可以通过检查alpha

func alphaAtPoint(point: CGPoint) -> CGFloat { 
    var pixel: [UInt8] = [0, 0, 0, 0] 
    let colorSpace = CGColorSpaceCreateDeviceRGB(); 
    let alphaInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue) 
    let context = CGBitmapContextCreate(&pixel, 1, 1, 8, 4, colorSpace, alphaInfo) 

    CGContextTranslateCTM(context, -point.x, -point.y); 

    self.layer.renderInContext(context) 

    return CGFloat(pixel[3]) 
} 
0

在故事板中,无论您希望相等长度,只需拖动到其他对象并选择“等宽”和“等长”即可。关于重叠,可能要么改变按钮或视图的顺序,要么重新创建它。