2013-09-25 45 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' –