Round Rect按钮是否有一种方法可以获得完全相同的图像大小? [英] Is there a way the Round Rect button to take exactly the same size of an image?

查看:158
本文介绍了Round Rect按钮是否有一种方法可以获得完全相同的图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Round Rect按钮是否有一种方法可以拍摄完全相同的图像尺寸?是否有圆形按钮?我有一个带有许多按钮图像的项目,它们混合在一起。图像大多是圆形的,按钮是矩形的,所以当我把它们放在一起时它们会混合在一起。

Is there a way the Round Rect button to take exactly the same size of an image?Are there any round buttons? I have a project with many buttons-images and they get mixed together. The images are mostly circular and the buttons Rectangular, so when I place them close to each other they get mixed.

推荐答案

当iPhone检测到屏幕上的触摸,它使用点击测试找到触摸的视图。默认情况下,命中测试假定每个视图都是一个矩形。

When the iPhone detects a touch on the screen, it finds the touched view using "hit testing". By default, hit testing assumes that each view is a rectangle.

如果您希望命中测试将视图视为不同的形状,则需要创建一个子类(在你的情况下 UIButton 并覆盖 pointInside:withEvent:方法来测试你想要使用的形状。

If you want hit testing to treat your view as a different shape, you need to create a subclass (of UIButton in your case) and override the pointInside:withEvent: method to test the shape you want to use.

例如:

@implementation MyOvalButton

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:self.bounds];
    return [path containsPoint:point];
}

我还没有测试过该代码。

I haven't tested that code.

Swift版本:

class MyOvalButton: UIButton {

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        return UIBezierPath(ovalIn: bounds).contains(point)
    }

不要忘记在故事板或xib中将按钮的自定义类设置为 MyOvalButton ,如果这是您创建按钮的位置。

Don't forget to set your button's custom class to MyOvalButton in your storyboard or xib, if that's where you create the button.

这是一个演示,我已经连接按钮的触摸和触摸事件,以便在触摸按钮时将背景变为灰色:

Here's a demo, where I have connected the touch-down and touch-up events of the button to turn the background gray when the button is touched:

这篇关于Round Rect按钮是否有一种方法可以获得完全相同的图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆