OC畫一個月亮??

iOS畫月亮

主要思路

1.CAShapeLayer + UIBezierPath

2.兩個弧線的關(guān)系是個數(shù)學(xué)問題

我的實現(xiàn)是,月亮的兩個端點連線作為內(nèi)弧的圓半徑屎勘,但感覺不是很漂亮另萤,跟月亮實際有些偏離。希望有朋友有更好方法蒋失。

3.難點在計算,內(nèi)弧圓的圓心和半徑桐玻,畫畫圖結(jié)合點數(shù)學(xué)基礎(chǔ)就OK了篙挽。

//
//  ViewController.m
//  MoonProgressDemo
//
//  Created by Robin on 2019/8/4.
//  Copyright ? 2019 RobinCompany. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) CAShapeLayer *moonLayer;
@property (nonatomic, assign) CGFloat progress;
@property (weak, nonatomic) IBOutlet UISlider *slider;

@end

@implementation ViewController

#define ScreenW CGRectGetWidth([UIScreen mainScreen].bounds)
#define ScreenH CGRectGetHeight([UIScreen mainScreen].bounds)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addLayer];
    _slider.layer.anchorPoint = CGPointMake(1, 1);
    _slider.transform = CGAffineTransformMakeRotation(- M_PI_2);
}

- (void)addLayer {
    CAShapeLayer *moon = [CAShapeLayer layer];
    CGFloat quardW = 0.25 * ScreenW;
    moon.frame = CGRectMake(quardW, 0.25 * ScreenH, 2 * quardW, 2 * quardW);
    _moonLayer = moon;
//    _moonLayer.backgroundColor = [UIColor redColor].CGColor;
    [self.view.layer addSublayer:moon];
    moon.strokeColor = [UIColor redColor].CGColor;
    moon.fillColor = nil;
    CGFloat x0 = 0.5 * CGRectGetWidth(_moonLayer.bounds);
    CGFloat y0 = 0.5 * CGRectGetHeight(_moonLayer.bounds);
    
    CGPoint center = CGPointMake(x0, y0);
    CGFloat radius = 0.5 * CGRectGetWidth(_moonLayer.bounds);
    CGFloat startAngle = 0.f;
    CGFloat endAngle = - M_PI;
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
//    moon.path = path.CGPath;
    CGPoint center1 = CGPointMake(x0, y0 - radius / (tan(M_PI / 6.0f)));
    
    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center1 radius:2.0f * radius startAngle:2.0f * M_PI / 3.0f endAngle:M_PI / 3.0f clockwise:NO];
    
//    [path addArcWithCenter:center1 radius:2 * radius startAngle:4 * M_PI / 3 endAngle:5 * M_PI / 3 clockwise:NO];
    [path appendPath:path1];
    moon.path = path.CGPath;
    
}
- (IBAction)sliderSlide:(UISlider *)sender {
    self.progress = sender.value;
    _moonLayer.fillColor = [UIColor colorWithRed:0 green:0 blue:255 alpha:_progress].CGColor;
//    _moonLayer.anchorPoint
    _moonLayer.affineTransform = CGAffineTransformMakeRotation(_progress * M_PI / 6.0f);

}

- (void)setProgress:(CGFloat)progress {
    _progress = progress;
    CGFloat endAngle1 = (1.0f + progress) * M_PI;
    CGFloat x0 = 0.5 * CGRectGetWidth(_moonLayer.bounds);
    CGFloat y0 = 0.5 * CGRectGetHeight(_moonLayer.bounds);
    CGPoint center = CGPointMake(x0, y0);
    CGFloat radius = 0.5 * CGRectGetWidth(_moonLayer.bounds);
    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0.f endAngle:endAngle1 clockwise:YES];
    
    CGFloat r2 = 2.0f * cos(0.5 * progress * M_PI) * radius;
    CGFloat startAngle2 = (0.5 * progress + 2.0f / 3.0f) * M_PI;
    CGFloat endAngle2 = (0.5 * progress + 1.0f / 3.0f) * M_PI;
    CGFloat distance = radius * sin (0.5 * progress *M_PI) + r2 * cos(M_PI / 6);
    CGFloat deltaX = distance * sin(0.5 * progress * M_PI);
    CGFloat deltaY = distance * cos(0.5 * progress * M_PI);
    CGPoint center2 = CGPointMake(x0 + deltaX, y0 - deltaY);
    UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center2 radius:r2 startAngle:startAngle2 endAngle:endAngle2 clockwise:NO];
    [path1 appendPath:path2];
    _moonLayer.path = path1.CGPath;
}
@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市镊靴,隨后出現(xiàn)的幾起案子铣卡,更是在濱河造成了極大的恐慌观腊,老刑警劉巖,帶你破解...
    沈念sama閱讀 223,002評論 6 519
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件算行,死亡現(xiàn)場離奇詭異梧油,居然都是意外死亡,警方通過查閱死者的電腦和手機州邢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,357評論 3 400
  • 文/潘曉璐 我一進店門儡陨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人量淌,你說我怎么就攤上這事骗村。” “怎么了呀枢?”我有些...
    開封第一講書人閱讀 169,787評論 0 365
  • 文/不壞的土叔 我叫張陵胚股,是天一觀的道長。 經(jīng)常有香客問我裙秋,道長琅拌,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,237評論 1 300
  • 正文 為了忘掉前任摘刑,我火速辦了婚禮进宝,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘枷恕。我一直安慰自己党晋,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,237評論 6 398
  • 文/花漫 我一把揭開白布徐块。 她就那樣靜靜地躺著未玻,像睡著了一般。 火紅的嫁衣襯著肌膚如雪胡控。 梳的紋絲不亂的頭發(fā)上扳剿,一...
    開封第一講書人閱讀 52,821評論 1 314
  • 那天,我揣著相機與錄音铜犬,去河邊找鬼舞终。 笑死,一個胖子當(dāng)著我的面吹牛癣猾,可吹牛的內(nèi)容都是我干的敛劝。 我是一名探鬼主播,決...
    沈念sama閱讀 41,236評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼纷宇,長吁一口氣:“原來是場噩夢啊……” “哼夸盟!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起像捶,我...
    開封第一講書人閱讀 40,196評論 0 277
  • 序言:老撾萬榮一對情侶失蹤上陕,失蹤者是張志新(化名)和其女友劉穎桩砰,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體释簿,經(jīng)...
    沈念sama閱讀 46,716評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡亚隅,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,794評論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了庶溶。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片煮纵。...
    茶點故事閱讀 40,928評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖偏螺,靈堂內(nèi)的尸體忽然破棺而出行疏,到底是詐尸還是另有隱情,我是刑警寧澤套像,帶...
    沈念sama閱讀 36,583評論 5 351
  • 正文 年R本政府宣布酿联,位于F島的核電站,受9級特大地震影響夺巩,放射性物質(zhì)發(fā)生泄漏贞让。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,264評論 3 336
  • 文/蒙蒙 一劲够、第九天 我趴在偏房一處隱蔽的房頂上張望震桶。 院中可真熱鬧休傍,春花似錦征绎、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,755評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至忙厌,卻和暖如春凫岖,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背逢净。 一陣腳步聲響...
    開封第一講書人閱讀 33,869評論 1 274
  • 我被黑心中介騙來泰國打工哥放, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人爹土。 一個月前我還...
    沈念sama閱讀 49,378評論 3 379
  • 正文 我出身青樓甥雕,卻偏偏與公主長得像,于是被迫代替她去往敵國和親胀茵。 傳聞我的和親對象是個殘疾皇子社露,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,937評論 2 361