【问题标题】:Create circle button in iOS7 without images在没有图像的iOS7中创建圆形按钮
【发布时间】:2013-10-01 05:41:05
【问题描述】:

我想重新创建 iOS7 时钟应用中的圆形按钮。根据按钮状态(绿色边框、红色边框、灰色填充),按钮基本上是具有不同外观的圆形。

我当然可以使用带有不同状态图像的简单 UIButton 来实现这一点。

但是我正在寻找一种以编程方式绘制圆的解决方案,因此我可以轻松更改半径、笔划宽度等。

据我所知,UIButton 只允许我为每个状态定义一个 UIImage,所以我不能直接修改每个状态的层(例如,提供一个带有cornerRadius 的层)。还有其他方法吗?

【问题讨论】:

  • 您是否考虑过绘制圆形视图并为其添加轻击手势?只是一个想法。
  • 是的,这可能会奏效。但是我想避免重新创建 UIButton 功能。因此,如果有一个带有 UIButton 的解决方案,我会更喜欢。

标签: ios uibutton ios7


【解决方案1】:

创建自定义按钮可能会有所帮助。

在.h文件中;

#import <UIKit/UIKit.h>
@interface CircleLineButton : UIButton

- (void)drawCircleButton:(UIColor *)color;

@end

在.m文件中;

    #import "CircleLineButton.h"

    @interface CircleLineButton ()

    @property (nonatomic, strong) CAShapeLayer *circleLayer;
    @property (nonatomic, strong) UIColor *color;
    @end

    @implementation CircleLineButton



    - (void)drawCircleButton:(UIColor *)color
    {
        self.color = color;

        [self setTitleColor:color forState:UIControlStateNormal];

        self.circleLayer = [CAShapeLayer layer];

        [self.circleLayer setBounds:CGRectMake(0.0f, 0.0f, [self bounds].size.width,
                                      [self bounds].size.height)];
        [self.circleLayer setPosition:CGPointMake(CGRectGetMidX([self bounds]),CGRectGetMidY([self bounds]))];

        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];

        [self.circleLayer setPath:[path CGPath]];

        [self.circleLayer setStrokeColor:[color CGColor]];

        [self.circleLayer setLineWidth:2.0f];
        [self.circleLayer setFillColor:[[UIColor clearColor] CGColor]];

        [[self layer] addSublayer:self.circleLayer];
    }

    - (void)setHighlighted:(BOOL)highlighted
    {
        if (highlighted)
        {
            self.titleLabel.textColor = [UIColor whiteColor];
            [self.circleLayer setFillColor:self.color.CGColor];
        }
        else
        {
            [self.circleLayer setFillColor:[UIColor clearColor].CGColor];
            self.titleLabel.textColor = self.color;
        }
    }

@end

在视图控制器中,调用[self.myCircleButton drawCircleButton:[UIColor myColor]]

【讨论】:

  • 我发现你需要使用[self.layer insertSublayer:self.circleLayer atIndex:0];在标题文本标签下方插入圆圈,否则按钮突出显示时在纯色下方。
  • stackoverflow.com/a/6954024/1091402 这是一个非常好的方法。它省去了处理点击检测、旋转、自动布局等的麻烦,只需将按钮做成圆形而不是添加额外的图层。
  • 这对我很有效。确保将按钮框架设置为正方形,否则圆形按钮将是椭圆形。您还应该将圆的路径缩进几个像素以考虑线宽。否则,您最终会在侧面有一些平坦的边缘。
【解决方案2】:

有很多方法可以做到这一点,例如:

  • 使用CAShapedLayer

  • 子类UIView并使用drawRect:方法画圆

  • 只要有一个正方形 UIView 并使用 layer.cornerRadius 财产。

根据您的需要,创建普通 UIButton 和调用这样简单的事情

myButton.layer.cornerRadius = myButton.bounds.size.width / 2.0;

可以工作(您需要包含 Quartz 框架)

【讨论】:

  • 是的,我可以很好地画一个圆圈,但是如何将不同的圆圈状态(红色、绿色、灰色填充)连接到 UIButton 状态?最好使用尽可能少的代码 :-) 我可以连接多个听众并相应地显示/隐藏圆圈,但这感觉有点矫枉过正。
  • 我认为最好的做法是创建一个普通的 UIButton,并添加一个 CAShapedLayer(带有圆形)作为该按钮层的 mask 属性。跨度>
  • 这会给我一个一般的圆形外观,但我无法为每个按钮状态定义不同的边框/填充颜色,对吧?我只能通过 setBackgroundImage:forState: 为每个状态提供一个 UIImage
【解决方案3】:

我用来实现这种事情的模式是:

继承UIButton 并实现drawRect 以根据需要绘制按钮,根据按钮的selectedhighlighted 属性自定义颜色。

然后覆盖 setSelectedsetHighlighted 以强制重绘,如下所示:

-(void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    [self setNeedsDisplay];
}

【讨论】:

    【解决方案4】:

    添加此代码

    imagePreview.layer.cornerRadius = (imagePreview.bounds.size.height/2);
    

    其中 imagePreview 是 UIview/UIimageview/UIButton

    别忘了添加

    #import <QuartzCore/QuartzCore.h>
    

    【讨论】:

    • 这是最简单最简单的解决方案。对我来说效果很好,而且实施起来超级快。
    【解决方案5】:

    快速扩展的解决方案:

    extension UIView{
        func asCircle(){
            self.layer.cornerRadius = self.frame.size.width / 2;
            self.layer.masksToBounds = true
        }
    }
    

    打电话就行了

    myView.asCircle()
    

    可以使用任何类型的视图,而不仅仅是按钮

    【讨论】:

    • 小更正:self.frame.size.width
    【解决方案6】:

    你可以使用这个控件,它是 UIButton 的子类。 https://github.com/alvaromb/AMBCircularButton

    【讨论】:

      【解决方案7】:

      我觉得效果不错。

      override func viewDidLoad() {
          super.viewDidLoad()
      
          var button = UIButton.buttonWithType(.Custom) as UIButton
          button.frame = CGRectMake(160, 100, 50, 50)
          button.layer.cornerRadius = 0.5 * button.bounds.size.width
          button.setImage(UIImage(named:"thumbsUp.png"), forState: .Normal)
          button.addTarget(self, action: "thumbsUpButtonPressed", forControlEvents: .TouchUpInside)
          view.addSubview(button)
      }
      
      func thumbsUpButtonPressed() {
          println("thumbs up button pressed")
      }
      

      【讨论】:

        【解决方案8】:
        1. 在情节提要中,制作方形 UIButton(例如 100 * 100 )

        2. 链接出口(例如。@property (Strong, nonatomic) UIButton IBOutlet * myButton;)

        3. 在您的代码中写下: self.myButton.layer.cornerRadius = 50; // 50 是正方形一侧长度的一半。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-04-24
          • 1970-01-01
          • 1970-01-01
          • 2017-03-19
          • 2019-12-24
          • 1970-01-01
          • 1970-01-01
          • 2018-09-23
          相关资源
          最近更新 更多