demo功能:ios 美元飄落效果的demo 。
demo說明:美元飄落效果demo ,可以多指觸控,觸控點越多,飄落的美元面額越大。主要代碼在:MainView.m中。
demo截屏:
demo代碼說明:
[csharp]
- (void)onTimer
{
UIImageView *dollarView;
UIImage *dollarImage;
// for effect... make it harder to get some of the other bill index's
// if it does not pass criteria reset back to 1
// double tap upside down for 5 Dollar bill
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;
}
//以上是檢測觸點,並分配不同的美元圖片
// for effect only draw one 2 dollar bill by resetting 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.x > 0.5f || deviceTilt.y > 0.5f || deviceTilt.x < -0.5f || deviceTilt.y < -0.5f) {
} 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];
}
}
- (void)onTimer
{
UIImageView *dollarView;
UIImage *dollarImage;
// for effect... make it harder to get some of the other bill index's
// if it does not pass criteria reset back to 1
// double tap upside down for 5 Dollar bill
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;
}
//以上是檢測觸點,並分配不同的美元圖片
// for effect only draw one 2 dollar bill by resetting 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.x > 0.5f || deviceTilt.y > 0.5f || deviceTilt.x < -0.5f || deviceTilt.y < -0.5f) {
} 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];
}
}