本文分享一個美元鈔票飄落的小 Demo,適合成為粒子效果的入門練習。
效果是鈔票從屏幕外飄入、旋轉下落。支持多指觸控,觸點越多,飄落的鈔票面額越大——通過設備的傾斜方向(deviceTilt)來決定面額,並據此選擇不同的紙鈔圖片。核心邏輯都在 MainView.m 的 onTimer 方法中。
- (void)onTimer
{
UIImageView *dollarView;
UIImage *dollarImage;
// 依設備傾斜方向決定面額,未命中條件則回落為 1 美元
if (billIndex > 2) {
if (deviceTilt.y > 0.5f)
billIndex = 5;
else
billIndex = 1;
} else if (billIndex > 1) {
if (deviceTilt.x > 0.5f)
billIndex = 3;
else if (deviceTilt.x < -0.5f)
billIndex = 4;
else
billIndex = 2;
}
switch (billIndex) {
case 2:
dollarImage = [UIImage imageNamed:@"dol2.png"];
break;
case 3:
dollarImage = [UIImage imageNamed:@"5dol.jpg"];
break;
case 4:
dollarImage = [UIImage imageNamed:@"10dol.jpg"];
break;
case 5:
dollarImage = [UIImage imageNamed:@"20dol.jpg"];
break;
default:
dollarImage = [UIImage imageNamed:@"dollar.png"];
break;
}
// 為效果計,2 美元只畫一張,用後重置 billIndex
if (billIndex == 2)
billIndex = 1;
// 動畫主體
dollarView = [[UIImageView alloc] initWithImage:dollarImage];
int scale = RANDOM_INT(1, 5);
int width = 100 * scale;
int height = 44 * scale;
if (useEmitterRect) {
dollarView.frame = emitterRect;
} else if (deviceTilt.y > 0.5f) {
dollarView.frame = CGRectMake(160 - (width / 2), 490, width, height);
} else if (deviceTilt.y < -0.5f) {
dollarView.frame = CGRectMake(160 - (width / 2), -(height + 10), width, height);
} else if (deviceTilt.x > 0.5f) {
dollarView.frame = CGRectMake(-(width + 10), 240 - (height / 2), width, height);
} else if (deviceTilt.x < -0.5f) {
dollarView.frame = CGRectMake(330, 240 - (height / 2), width, height);
} else {
// 無傾斜時從四個角落隨機灑入
switch (RANDOM_INT(0, 3)) {
case 0:
dollarView.frame = CGRectMake(-300, -300, 100 * scale, 44 * scale);
break;
case 1:
dollarView.frame = CGRectMake(470, -150, 100 * scale, 44 * scale);
break;
case 3:
dollarView.frame = CGRectMake(470, 630, 100 * scale, 44 * scale);
break;
default:
dollarView.frame = CGRectMake(-150, 630, 100 * scale, 44 * scale);
break;
}
}
[self addSubview:dollarView];
if (helpView)
[self bringSubviewToFront:helpView];
[UIView beginAnimations:nil context:dollarView];
[UIView setAnimationDuration:2];
dollarView.frame = CGRectMake(RANDOM_INT(-50, 370), RANDOM_INT(-25, 665), 100, 44);
int radians = RANDOM_INT(0, 360) * 0.0174532925;
[dollarView setTransform:CGAffineTransformMakeRotation(radians)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
billCount++;
[billList addObject:dollarView];
// 超過上限則讓最老的一張淡出移除,控制同時存在的數量
if ([billList count] >= MAX_BILLS) {
UIImageView *oldBill = (UIImageView *)[billList objectAtIndex:0];
[billList removeObjectAtIndex:0];
[UIView beginAnimations:nil context:oldBill];
[UIView setAnimationDuration:2];
[oldBill setAlpha:0.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(goPutThemBack:finished:context:)];
[UIView commitAnimations];
}
}
示例用的是 beginAnimations 這套老式 API,新項目請改用 UIView animateWithDuration: 的 block 版本,語法更清晰也不易出錯。其次,定時器每 0.1 秒就往視圖層添加一個子視圖,若無上限控制會迅速拖垮性能,示例用 MAX_BILLS 做數量閾值,並讓最老的鈔票淡出,這個思路值得借鑑。
再者,示例中的坐標值是硬編碼的(如 160、240、490),換到 iPhone 5 之後的屏幕比例會錯位,實戰應改為基於 bounds 的比例計算。若追求更真實的重力與碰撞手感,與其用定時器模擬,不如接入 UIDynamic 的重力與碰撞行為,或直接使用 CAEmitterLayer 粒子系統,代碼量與性能都會更理想。