0
A
回答
0
如果你知道你的弦位置可以通過設置剪輯區域然後填充圓來填充和絃。
要計算出和絃的位置以給出x%的面積,您需要執行一些幾何/三角函數。不通過中心的每一個弦與中心形成等腰三角形,並且該三角形的兩邊是半徑限定圓的一部分。因此,不通過中心的弦的一側的區域是三角形和段的面積的差異,並且您可以計算出和絃如何劃分該區域或者和絃需要如何劃分面積以特定的比例。
如果這一切聽起來像gobbledegook嘗試查找和絃幾何,你無疑會找到有用的圖表和公式的書籍/網站。
HTH
1
最簡單的方法是有兩個子視圖,一個爲綠色「百分比填充」水平,一個用於標籤的文本視圖。然後,您可以更新frame
以獲得「填充百分比」,顯然是基於您希望達到的百分比。然後將圓形面具應用於整個事情。
例如:
// CircleLevelView.h
//
// Created by Robert Ryan on 10/28/17.
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface CircleLevelView : UIView
/// Percent filled
///
/// Value between 0.0 and 1.0.
@property (nonatomic) IBInspectable CGFloat percent;
/// Text to show up in center of view
///
/// Value between 0.0 and 1.0.
@property (nonatomic, strong) IBInspectable NSString *text;
@end
而且
// CircleLevelView.m
//
// Created by Robert Ryan on 10/28/17.
#import "CircleLevelView.h"
@interface CircleLevelView()
@property (nonatomic, weak) CAShapeLayer *circleMask;
@property (nonatomic, weak) UILabel *label;
@property (nonatomic, weak) UIView *fillView;
@end
@implementation CircleLevelView
@synthesize percent = _percent;
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
[self configure];
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self configure];
return self;
}
- (instancetype)init {
return [self initWithFrame:CGRectZero];
}
- (void)configure {
self.clipsToBounds = true;
UILabel *fillView = [[UILabel alloc] init];
fillView.translatesAutoresizingMaskIntoConstraints = false;
fillView.backgroundColor = [UIColor colorWithRed:169.0/255.0
green:208.0/255.0
blue:66.0/255.0
alpha:1.0];
[self addSubview:fillView];
self.fillView = fillView;
UILabel *label = [[UILabel alloc] init];
label.translatesAutoresizingMaskIntoConstraints = false;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentCenter;
[self addSubview:label];
self.label = label;
[NSLayoutConstraint activateConstraints:@[
[label.topAnchor constraintEqualToAnchor:self.topAnchor],
[label.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
[label.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
[label.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
[fillView.topAnchor constraintEqualToAnchor:self.topAnchor],
[fillView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
[fillView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
[fillView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor]
]];
CAShapeLayer *circleMask = [CAShapeLayer layer];
circleMask.fillColor = [UIColor whiteColor].CGColor;
circleMask.strokeColor = [UIColor blackColor].CGColor;
circleMask.lineWidth = 0;
self.layer.mask = circleMask;
self.circleMask = circleMask;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGPoint center = CGPointMake(self.bounds.origin.x + self.bounds.size.width/2.0, self.bounds.origin.y + self.bounds.size.height/2.0);
CGFloat radius = MIN(self.bounds.size.width, self.bounds.size.height)/2.0;
self.circleMask.path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:M_PI * 2.0 clockwise:true].CGPath;
[self updatePercentFill];
}
- (void)updatePercentFill {
CGFloat circleHeight = MIN(self.bounds.size.width, self.bounds.size.height);
CGFloat percentHeight = circleHeight * self.percent;
self.fillView.frame = CGRectMake(0,
(self.bounds.size.height - circleHeight)/2 + (circleHeight - percentHeight),
self.bounds.size.width,
circleHeight);
}
// MARK: - Custom Accessor Methods
- (CGFloat)percent {
return _percent;
}
- (void)setPercent:(CGFloat)percent {
_percent = percent;
[self updatePercentFill];
}
- (NSString *)text {
return self.label.text;
}
- (void)setText:(NSString *)text {
self.label.text = text;
}
@end
國債收益率:
相關問題
- 1. 畫一個圓圈像素
- 2. 如何在Tkinter的標籤上畫一個圓圈?
- 3. 在距離10處的圓圈內畫一個圓圈
- 4. 畫一個圓圈在Android MapView的
- 5. 畫一個填滿的圓圈
- 6. 如何在一個圓圈
- 7. 讓用戶只畫出一個圓圈作爲源,但一些圓圈用於目標
- 8. 如何在一個大圓圈中均勻分佈12個圓圈
- 9. 如何在WPF中創建一個帶圓圈的圓圈?
- 10. 在視圖上畫一個圓圈(android)
- 11. 在mapkit上畫一個圓圈
- 12. 我想在VB.Net中畫一個圓圈
- 13. 在android中畫一個圓圈
- 14. 畫一個圓圈,反充Android中
- 15. 在gisMap上畫一個圓圈
- 16. 如何,畫一個圓圈上的變化的背景
- 17. 如何遮擋畫布中的圓圈
- 18. iOS - 如何畫一個數字/字符的圓圈?
- 19. 如何清除畫布中圓圈的前一個位置?
- 20. PowerBI Guage - 百分比分割爲目標
- 21. 在一個圓圈內表示一個點的位置,以任意段的百分比表示
- 22. Chart.js 2.0甜甜圈提示百分比
- 23. Android圓圈複選標記動畫
- 24. 如何畫一個圓圈,並填充顏色
- 25. 如何在一個圓圈中製作java形狀動畫?
- 26. 如何使用PlayN爲一個圓圈設置動畫?
- 27. 如何使用KineticJS從一個圓圈創建箭頭到另一個圓圈?
- 28. 如何在opengles中畫圓圈
- 29. 如何在圓圈之間畫線?
- 30. 如何讓角度js中的百分比進度圈工作?
獲取介紹可可繪圖。它被命名爲「Cocoa Drawing Guide」。 –