欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios 美元飘落效果的demo

程序员文章站 2022-08-05 22:22:12
demo功能:ios 美元飘落效果的demo 。 demo说明:美元飘落效果demo ,可以多指触控,触控点越多,飘落的美元面额越大。主要代码在:mainview.m中。 de...

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];
 }
 
}