【簡易版】IOS仿periscope自製狂贊飄桃心

來源:互聯網
上載者:User

標籤:

periscope自製狂贊飄桃心

國外的IOS app“periscope”非常的火,觀看手機ApsaraVideo for Live的時候,點擊螢幕任何一個地方,螢幕右下角就能飄出各種顏色的桃心,效果非常的炫!

為此我自製了一個仿periscope桃心的代碼:

- (void) StartLittleHeartShow{

    // 構造一個小桃心的UIImageView,其中桃心的顏色可以隨機變化

    float fColorRedBase   = random()%10/10.0;

    float fColorGreenBase = random()%5/10.0;

    float fColorBlueBase  = random()%5/10.0;

    float fAlphaBase      = 1.0 - random()%7/10.0;

    //小桃心的顏色

    UIColor* heartColor = [[UIColor alloc] initWithRed:fColorRedBase green:fColorGreenBase blue:fColorBlueBase alpha:fAlphaBase];

    //向UIImage的桃心圖片中填入隨機顏色 

    UIImage* flakeImage = [[UIImage imageNamed:@"PooHeart.png"] imageWithTintColor:heartColor];

    UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];

    float fXBase = HEART_BK_VIEW_WIDTH/2;//HEART_BK_VIEW_WIDTH是表單view的寬度

    float fYBase = HEART_BK_VIEW_HEIGHT;//HEART_BK_VIEW_HEIGHT是表單view的高度

 

    // 設定小桃心動畫起始點,X位置向右隨機位移0~19

    long lRandom = random();

    int startX = fXBase+lRandom%20;

    int startY = fYBase;

    //設定小桃心動畫結束點,X位置左右位移0~74

    int endX = ((lRandom%2)==0) ? (startX - lRandom%75) : (startX + lRandom%75);

    double scale = 1 / round(random() % 100) + 1.0;//設定桃心大小的隨機位移,這樣出來的桃心大小就可以不一樣

    double speed = 1 / round(random() % 100) + 1.0;//設定桃心飛行的速度位移,這樣每個桃心飛出來的速度就可以不一樣

    

    scale = (scale > 1.5) ? 1.5 : scale;

 

    flakeView.frame = CGRectMake(startX, startY, 25.0 * scale, 25.0 * scale);//初始化桃心的frame

    

    @try {

        // 把該桃心加入到主視圖中,注意在動畫完成後,需要把這個桃心從主視圖中remove掉

        [self.view addSubview:flakeView];

        

        [UIView beginAnimations:nil context:(__bridge void *)(flakeView)];

        // 設定桃心飛行的時間,也就是其飛行的速度

        float fSpeedBase = random()%5;

        fSpeedBase = (fSpeedBase < 3.0) ? 3.0 : fSpeedBase;

        float fDuration = fSpeedBase * speed;

        fDuration = (fDuration > 5.0) ? 5.0 : fDuration;

        fDuration = (fDuration <= 0) ? 2.5 : fDuration;

        fDuration = fDuration - 1;

        [UIView setAnimationDuration:fDuration];

        

        // 設定桃心的飛行終點!

        flakeView.frame = CGRectMake(endX, fYBase-HEART_BK_VIEW_HEIGHT-random()%50, 25.0 * scale, 25.0 * scale);

        

        // 設定桃心動畫結束後的callback函數,需要在callback中將flakeView移除self.view

        [UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];

        [UIView setAnimationDelegate:self];

        [UIView commitAnimations];//開始動畫

    }

    @catch (NSException *exception) {

        NSLog(@"StartLoveShow exception...");

    }

}

 //在動畫結束後,onAnimationComplete函數中移除flakeView

- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

    UIImageView *flakeView = (__bridge UIImageView *)(context);

    flakeView.hidden = YES;

    [flakeView removeFromSuperview];

}

//最後放入一個全屏的click事件中,就可以了,點擊任何一個地方都可以出桃心

 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    [self StartLoveShow];

}

 

【簡易版】IOS仿periscope自製狂贊飄桃心

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.