2013-09-25 39 views
14

我想重新创建在iOS7中的时钟应用程序中找到的圆形按钮。这些按钮基本上是根据按钮状态(绿色边框,红色边框,灰色填充)具有不同外观的圆形。在没有图像的iOS7中创建圆形按钮

我当然可以用一个简单的UIButton来实现这个功能,并带有不同状态的图像。

但是我寻找其绘制圆编程的溶液,所以可以很容易地改变半径,笔划宽度等

据我可以看到的UIButton只允许我限定的UIImage对于每个状态,所以我不能直接修改每个状态的图层(例如,用cornerRadius提供一个图层)。有另一种方法吗?

+0

你有没有考虑绘制一个圆形查看并添加轻拍手势?只是一个想法。 – Douglas

+0

是的,这可能会起作用。但是我想避免重新创建UIButton功能。所以如果有一个UIButton的解决方案,我宁愿这样做。 – henning77

回答

11

有很多,你可以做到这一点的方式,例如:

  • 使用CAShapedLayer

  • 子类的UIView和使用的drawRect:方法来绘制一个圆

  • 只是有一个方块UIView并使用layer.cornerRadius 属性。

根据您的需求,为创造正常的UIButton并调用

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

可以工作,简单的东西(你需要包括石英框架)

+1

是的,我可以绘制一个圆圈,但我如何将不同的圆形状态(红色,绿色,灰色填充)连接到UIButton状态?最好用尽可能少的代码:-)我可以连接几个听众并相应地显示/隐藏这些圈子,但这感觉有点矫枉过正。 – henning77

+0

我认为最好的方法是创建一个普通的UIButton,并添加一个CAShapedLayer(带圆形)作为该按钮图层的* mask *属性。 –

+0

这会给我一个普通的圆形外观,但我无法为每个按钮状态定义不同的边框/填充颜色,对吧?我只能通过setBackgroundImage提供每个状态的UIImage:forState: – henning77

30

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

在.h文件中;

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

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

@end 

in .m file;

#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]]

+2

我发现你需要使用'[self.layer insertSublayer:self.circleLayer atIndex:0];'在标题文本标签下插入圆圈,否则按钮突出显示时,在纯色下方。 –

+1

http://stackoverflow.com/a/6954024/1091402这是一个非常优越的方式。通过简单地按下按钮而不是添加额外的图层,它可以为您节省处理命中检测,旋转,自动布局等问题。 – Rick

+0

这适用于我。一定要让你的按钮框为正方形,否则你的圆形按钮将变成椭圆形。您还应该缩进圆圈的几个像素的路径来说明lineWidth。否则,你会得到一些边缘平坦的边缘。 –

4

我用来实现这种东西的模式是:

子类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> 
+0

这是最简单和最简单的解决方案。为我工作得很好,超级快速实施。 – Nerrolken

0

我认为它很好。

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") 
} 
-1
  1. 在情节串连图板,让正方形的UIButton(例如,100 * 100)。

  2. 链接出口(例如@property(强,非原子)的UIButton IBOutlet中*为myButton;)

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

4

解决方案以迅速的扩展:

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

只需拨打

myView.asCircle() 

可与任何类型来看,不仅是一个按钮,工作

+1

小修正:'self.frame.size.width' –