【问题标题】:Create an UIButton with a particular shape: how? [duplicate]创建具有特定形状的 UIButton:如何? [复制]
【发布时间】:2024-01-17 18:37:01
【问题描述】:

可能重复:
Simple way of using irregular shaped buttons

我找到了有关创建矩形或圆形 UIButton 的示例。

是否可以创建具有特定形状(例如心形、星形等)的 UIButton?

怎么样?

【问题讨论】:

    标签: objective-c uibutton


    【解决方案1】:

    最简单的方法是使用:

    UIButton *myButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [myButton setFrame:CRectMake( 0, 0, imWidth, imHeight)];
    [myButton setBackgroundImage:[UIImage imageNamed:@"shabadabada.png"] forState:UIControlStateNormal];
    

    这将为您提供一个简单的按钮......虽然它实际上并不像“shabadabada”(:P),但它将是一个方形按钮。

    要制作真正自定义形状的“按钮”,您必须从 UIView 类中实现:

    -(void) drawRect:(CGRect)rect;
    

    你必须逐点绘制你的形状,这里有一个关于this的简单指南。

    同时也响应以下方法:

    – touchesBegan:withEvent:
    – touchesMoved:withEvent:
    – touchesEnded:withEvent:
    – touchesCancelled:withEvent:
    

    或者只是为了:

    – addGestureRecognizer:
    

    【讨论】:

      最近更新 更多