我想重新創建在iOS7中的時鐘應用程序中找到的圓形按鈕。這些按鈕基本上是根據按鈕狀態(綠色邊框,紅色邊框,灰色填充)具有不同外觀的圓形。在沒有圖像的iOS7中創建圓形按鈕
我當然可以用一個簡單的UIButton來實現這個功能,並帶有不同狀態的圖像。
但是我尋找其繪製圓編程的溶液,所以可以很容易地改變半徑,筆劃寬度等
據我可以看到的UIButton只允許我限定的UIImage對於每個狀態,所以我不能直接修改每個狀態的圖層(例如,用cornerRadius提供一個圖層)。有另一種方法嗎?
我想重新創建在iOS7中的時鐘應用程序中找到的圓形按鈕。這些按鈕基本上是根據按鈕狀態(綠色邊框,紅色邊框,灰色填充)具有不同外觀的圓形。在沒有圖像的iOS7中創建圓形按鈕
我當然可以用一個簡單的UIButton來實現這個功能,並帶有不同狀態的圖像。
但是我尋找其繪製圓編程的溶液,所以可以很容易地改變半徑,筆劃寬度等
據我可以看到的UIButton只允許我限定的UIImage對於每個狀態,所以我不能直接修改每個狀態的圖層(例如,用cornerRadius提供一個圖層)。有另一種方法嗎?
有很多,你可以做到這一點的方式,例如:
使用CAShapedLayer
子類的UIView和使用的drawRect:方法來繪製一個圓
只是有一個方塊UIView並使用layer.cornerRadius 屬性。
根據您的需求,爲創造正常的UIButton並調用
myButton.layer.cornerRadius = myButton.bounds.size.width/2.0;
可以工作,簡單的東西(你需要包括石英框架)
創建自定義按鈕可能會有所幫助。
在.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]]
我發現你需要使用'[self.layer insertSublayer:self.circleLayer atIndex:0];'在標題文本標籤下插入圓圈,否則按鈕突出顯示時,在純色下方。 –
http://stackoverflow.com/a/6954024/1091402這是一個非常優越的方式。通過簡單地按下按鈕而不是添加額外的圖層,它可以爲您節省處理命中檢測,旋轉,自動佈局等問題。 – Rick
這適用於我。一定要讓你的按鈕框爲正方形,否則你的圓形按鈕將變成橢圓形。您還應該縮進圓圈的幾個像素的路徑來說明lineWidth。否則,你會得到一些邊緣平坦的邊緣。 –
我用來實現這種東西的模式是:
子類UIButton
和實施drawRect
只要你想繪製按鈕,根據按鈕的selected
和highlighted
屬性定製顏色。
然後覆蓋setSelected
和setHighlighted
強制重繪像這樣:
-(void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
[self setNeedsDisplay];
}
添加該代碼
imagePreview.layer.cornerRadius = (imagePreview.bounds.size.height/2);
其中imagePreview是的UIView /的UIImageView/UIButton的
不要忘記添加
#import <QuartzCore/QuartzCore.h>
這是最簡單和最簡單的解決方案。爲我工作得很好,超級快速實施。 – Nerrolken
你可以使用這個控件,它是UIButton的子類。 https://github.com/alvaromb/AMBCircularButton
我認爲它很好。
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")
}
在情節串連圖板,讓正方形的UIButton(例如,100 * 100)。
鏈接出口(例如@property(強,非原子)的UIButton IBOutlet中*爲myButton;)
在你的代碼中寫下: self.myButton.layer.cornerRadius = 50; // 50是一邊正方形長度的一半。
解決方案以迅速的擴展:
extension UIView{
func asCircle(){
self.layer.cornerRadius = self.frame.size.width/2;
self.layer.masksToBounds = true
}
}
只需撥打
myView.asCircle()
可與任何類型來看,不僅是一個按鈕,工作
小修正:'self.frame.size.width' –
你有沒有考慮繪製一個圓形查看並添加輕拍手勢?只是一個想法。 – Douglas
是的,這可能會起作用。但是我想避免重新創建UIButton功能。所以如果有一個UIButton的解決方案,我寧願這樣做。 – henning77