periscope自制狂赞飘桃心
国外的IOS app“periscope”很的火,观看手机视频直播的时候,点击屏幕不论什么一个地方,屏幕右下角就能飘出各种颜色的桃心,效果很的炫!
为此我自制了一个仿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:fColorBlueBasealpha: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];
}