iOS中彈簧動(dòng)畫Spring

慢動(dòng)作下動(dòng)畫

iOS動(dòng)畫.gif
@implementation DZAnimatedController {
    
    UIView *_view1;
    UIView *_view2;
    UIView *_view3;
    BOOL selected;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"AnimatedController";
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake((CGRectGetWidth(self.view.frame)-60)/2, CGRectGetHeight(self.view.frame)-80, 60, 60)];
    view1.backgroundColor = [UIColor greenColor];
    [self.view addSubview:view1];
    [view1.layer setCornerRadius:30];
    [view1.layer setMasksToBounds:YES];
    _view1 = view1;
    _view1.alpha = 0;
  
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake((CGRectGetWidth(self.view.frame)-60)/2, CGRectGetHeight(self.view.frame)-80, 60, 60)];
    view2.backgroundColor = [UIColor blueColor];
    [self.view addSubview:view2];
    [view2.layer setCornerRadius:30];
    [view2.layer setMasksToBounds:YES];
    _view2 = view2;
    _view2.alpha = 0;
    
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake((CGRectGetWidth(self.view.frame)-60)/2, CGRectGetHeight(self.view.frame)-80, 60, 60)];
    view3.backgroundColor = [UIColor redColor];
    [self.view addSubview:view3];
    [view3.layer setCornerRadius:30];
    [view3.layer setMasksToBounds:YES];
    _view3 = view3;
    _view3.alpha = 0;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake((CGRectGetWidth(self.view.frame)-60)/2, CGRectGetHeight(self.view.frame)-80, 60, 60);
    btn.backgroundColor = [UIColor grayColor];
    [self.view addSubview:btn];
    [btn.layer setCornerRadius:30];
    [btn.layer setMasksToBounds:YES];
    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    /**
     iOS中的動(dòng)畫就是圍繞著UIView的幾個(gè)屬性來玩的, frame, bounds, center, background, transform, alpha, coententStretch,
     iOS中動(dòng)畫的方式有三種:
     1. UIView(UIViewAnimation): 也成首尾式動(dòng)畫
     
     2. UIView(UIViewAnimationWithBlock): block動(dòng)畫, iOS中最常用, 是對(duì)首尾式動(dòng)畫的進(jìn)一步封裝.
     
     3. CoreAnimation: 主要作用CALayer
     */
    
    //通過(描述彈簧的運(yùn)動(dòng))時(shí)間曲線 來 執(zhí)行動(dòng)畫, 當(dāng)阻尼比為1的時(shí)候, 該動(dòng)畫 將(沒有任何震蕩地)平穩(wěn)的減速到最終的模型值,
    //阻尼比小于1的話,將反復(fù)震蕩直到完全停止.
    /* Performs `animations` using a timing curve described by the motion of a spring. When `dampingRatio` is 1, the animation will smoothly 

decelerate to its final model values without oscillating. Damping ratios less than 1 will oscillate more and more before coming to a complete stop. 

You can use the initial spring velocity to specify how fast the object at the end of the simulated spring was moving before it was attached. It's a unit 

coordinate system, where 1 is defined as travelling the total animation distance in a second. So if you're changing an object's position by 200pt in 

this animation, and you want the animation to behave as if the object was moving at 100pt/s before the animation started, you'd pass 0.5. You'll 

typically want to pass 0 for the velocity. */

    //timing curve 時(shí)間曲線
    //motion of spring 彈簧的運(yùn)動(dòng)(移動(dòng))
    //dampingRato 阻尼比
    //oscillating 震蕩
}

- (void)clickBtn:(UIButton *)btn {
    
    [UIView animateWithDuration:.5 delay:0 usingSpringWithDamping:.4 initialSpringVelocity:.6 options:UIViewAnimationOptionCurveLinear animations:^{

        if(!selected) {
            
            _view1.alpha = 1;
            _view2.alpha = 1;
            _view3.alpha = 1;
            _view1.transform =  CGAffineTransformMakeTranslation(-80, -100);
            _view2.transform = CGAffineTransformMakeTranslation(0, -100);
            _view3.transform = CGAffineTransformMakeTranslation(80, -100);
            
        } else {
            
            _view1.alpha = 0;
            _view2.alpha = 0;
            _view3.alpha = 0;
            _view1.transform = CGAffineTransformIdentity;
            _view2.transform = CGAffineTransformIdentity;
            _view3.transform = CGAffineTransformIdentity;
        }
        [btn setEnabled:NO];
    } completion:^(BOOL finished) {

        selected = !selected;
        [btn setEnabled:YES];
    }];

}

@end
iOS動(dòng)畫.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末库倘,一起剝皮案震驚了整個(gè)濱河市茂缚,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖擎值,帶你破解...
    沈念sama閱讀 222,590評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件麻顶,死亡現(xiàn)場(chǎng)離奇詭異狡汉,居然都是意外死亡甸赃,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門趟据,熙熙樓的掌柜王于貴愁眉苦臉地迎上來券犁,“玉大人,你說我怎么就攤上這事汹碱≌吵模” “怎么了?”我有些...
    開封第一講書人閱讀 169,301評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵咳促,是天一觀的道長(zhǎng)稚新。 經(jīng)常有香客問我,道長(zhǎng)跪腹,這世上最難降的妖魔是什么褂删? 我笑而不...
    開封第一講書人閱讀 60,078評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮冲茸,結(jié)果婚禮上屯阀,老公的妹妹穿的比我還像新娘。我一直安慰自己轴术,他們只是感情好蹲盘,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著膳音,像睡著了一般。 火紅的嫁衣襯著肌膚如雪铃诬。 梳的紋絲不亂的頭發(fā)上祭陷,一...
    開封第一講書人閱讀 52,682評(píng)論 1 312
  • 那天苍凛,我揣著相機(jī)與錄音,去河邊找鬼兵志。 笑死醇蝴,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的想罕。 我是一名探鬼主播悠栓,決...
    沈念sama閱讀 41,155評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼按价!你這毒婦竟也來了惭适?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,098評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤楼镐,失蹤者是張志新(化名)和其女友劉穎癞志,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體框产,經(jīng)...
    沈念sama閱讀 46,638評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡凄杯,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了秉宿。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片戒突。...
    茶點(diǎn)故事閱讀 40,852評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖描睦,靈堂內(nèi)的尸體忽然破棺而出膊存,到底是詐尸還是另有隱情,我是刑警寧澤酌摇,帶...
    沈念sama閱讀 36,520評(píng)論 5 351
  • 正文 年R本政府宣布膝舅,位于F島的核電站窑多,受9級(jí)特大地震影響仍稀,放射性物質(zhì)發(fā)生泄漏埂息。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評(píng)論 3 335
  • 文/蒙蒙 一千康、第九天 我趴在偏房一處隱蔽的房頂上張望享幽。 院中可真熱鬧,春花似錦拾弃、人聲如沸值桩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至咳秉,卻和暖如春婉支,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背澜建。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工向挖, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留炕舵,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,279評(píng)論 3 379
  • 正文 我出身青樓幕侠,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親晤硕。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容