iOS 禮物動畫效果

最近項目中要使用直播功能,自然就涉及禮物效果展示。示例效果如下圖

動畫效果.gif

主要是解決2個問題:
1.動畫效果。
2.送到多次禮物后禮物按收到順序播放動畫。

思路:

1.針對第一個問題,使用UIView的動畫即可做出。
2.第二個問題要實時監(jiān)聽新加入的動畫并且按順序執(zhí)行,自然就想到用隊列NSOperationQueue處理。第一步,創(chuàng)建一個最大并發(fā)數(shù)為1的隊列(滿足FIFO),這樣可以使動畫按順序執(zhí)行。但是出現(xiàn)了新的問題:每個Operation中的代碼也是異步的!這樣導(dǎo)致動畫并不是一個執(zhí)行完了再接著一個執(zhí)行。于是想到一個投機(jī)的辦法,讓線程sleep......

核心代碼很簡單,主要是用的UIView的動畫效果,直接看代碼

@interface ViewController ()

@property (strong, nonatomic) NSOperationQueue *queue;

@end

@implementation ViewController

- (NSOperationQueue *)queue
{
    if (!_queue)
    {
        self.queue = [[NSOperationQueue alloc] init];
        //最大并發(fā)數(shù)量為1,實現(xiàn)FIFO
         self.queue.maxConcurrentOperationCount = 1;
    }

    return _queue;
}

//點擊一次模擬收到一次禮物
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^
    {
        //根據(jù)具體的邏輯展示不同的動畫
        [self receiveGift:@"mogu"];
    }];
    
    [self.queue addOperation:op];
}

//執(zhí)行動畫
- (void)receiveGift:(NSString *)giftName
{
    CGFloat duringTime = 4;
    CGFloat WH = 100;
    CGFloat leftOffSet = 50;

    //動畫效果需要在主線程中進(jìn)行
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^
    {
        UIImageView *aImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-WH, 0, WH, WH)];
        aImageView.image = [UIImage imageNamed:giftName];
        [self.view addSubview:aImageView];
    
        CGAffineTransform zoomInScaleTransform = CGAffineTransformMakeScale(0.5, 0.5);
        aImageView.transform = zoomInScaleTransform;
    
    
        [UIView animateKeyframesWithDuration:duringTime delay:0 options:0 animations:^
        {
           [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:0.1 animations:^
           {
              aImageView.transform = CGAffineTransformIdentity;
              aImageView.center = CGPointMake(self.view.frame.size.width * 0.5 - leftOffSet, self.view.frame.size.height * 0.5 - 100);
           }];
         
           [UIView addKeyframeWithRelativeStartTime:0.1 relativeDuration:0.8 animations:^
            {
              aImageView.center = CGPointMake(self.view.frame.size.width * 0.5 + leftOffSet, self.view.frame.size.height * 0.5 - 100);
            }];
         
           [UIView addKeyframeWithRelativeStartTime:0.9 relativeDuration:0.1 animations:^
            {
              aImageView.center = CGPointMake(self.view.frame.size.width + WH,0);
              aImageView.transform = CGAffineTransformMakeScale(0.5, 0.5);
            }];
         
     }
     completion:^(BOOL finished)
     {
         [aImageView removeFromSuperview];
     }];
    
    });

    //線程休眠
    sleep(duringTime);
}

通過sleep讓線程休眠起到線程執(zhí)行時間為完整的一個動畫時間達(dá)到預(yù)期的效果。
大神若有更好的思路請告訴我??.....

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容