iOS 高仿《掌上生活》app 动画

来源:互联网 发布:c语言屯屯屯烫烫烫 编辑:程序博客网 时间:2024/04/28 01:12

前几天突然看到了招商银行的《掌上生活》app ,看到她的首页的动画还有点意思,然后我就模仿写了一个高仿的掌上生活。

具体用到了重力感应和CABasicAnimation方法:

代码如下:

[html] view plaincopy
  1. #import "ViewController.h"  
  2. #import <QuartzCore/QuartzCore.h>  
  3.   
  4.    
  5. #define Accelerometer_Notice_Windage  0.03  
  6. #define DegreesToRadians(x) (M_PI * x / 180.0)  
  7.   
  8.   
  9. @interface ViewController (){  
  10.   
  11.     UIView *rightDown;  
  12.     UIView *leftdown;  
  13.     UIView *rightUp;  
  14.     UIView *leftup;  
  15.       
  16.     float lastAccelerationX;  
  17.     float lastAccelerationY;  
  18.     float lastAccelerationZ;  
  19.       
  20.     NSMutableArray* animationViewArray;  
  21. }  
  22.   
  23. @end  
  24.   
  25. @implementation ViewController  
  26.   
  27. - (void)viewDidLoad  
  28. {  
  29.     [super viewDidLoad];  
  30.     // Do any additional setup after loading the view, typically from a nib.  
  31.       
  32.       [[UIAccelerometer sharedAccelerometer] setDelegate:self];  
  33.       lastAccelerationX=0;  
  34.       lastAccelerationY=0;  
  35.       lastAccelerationZ=0;  
  36.       animationViewArray=[[NSMutableArray alloc]init];  
  37.   
  38.       
  39.     self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];  
  40.     UIImageView *headerBG=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 46)];  
  41.     headerBG.image=[UIImage imageNamed:@"navigation.png"];  
  42.     headerBG.userInteractionEnabled=YES;  
  43.     [self.view   addSubview:headerBG];  
  44.     [headerBG release];  
  45.       
  46.       
  47.     UIImageView *cmblogo=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 88, 26)];  
  48.     cmblogo.image=[UIImage imageNamed:@"logo.png"];  
  49.     [headerBG   addSubview:cmblogo];  
  50.     [cmblogo release];  
  51.       
  52.       
  53.     UIImageView *titleimage=[[UIImageView alloc]initWithFrame:CGRectMake(110, 10, 93, 26)];  
  54.     titleimage.image=[UIImage imageNamed:@"font.png"];  
  55.     [headerBG   addSubview:titleimage];  
  56.     [titleimage release];  
  57.       
  58.       
  59.     UIButton *loginbutton=[UIButton buttonWithType:UIButtonTypeCustom];  
  60.     loginbutton.frame=CGRectMake(260,6, 50, 36);  
  61.     [loginbutton setBackgroundImage:[UIImage imageNamed:@"btm_nav.png"] forState:UIControlStateNormal];  
  62.     [loginbutton setBackgroundImage:[UIImage imageNamed:@"btm_nav.png"] forState:UIControlStateNormal];  
  63.     [loginbutton addTarget:self action:@selector(doanimation) forControlEvents:UIControlEventTouchUpInside];  
  64.     [loginbutton setTitle:@"登录" forState:UIControlStateNormal];  
  65.     [loginbutton setTitle:@"登录" forState:UIControlStateHighlighted];  
  66.     [headerBG addSubview:loginbutton];  
  67.       
  68.   
  69.     //便利生活view设置  
  70.     leftdown=[[UIImageView alloc]initWithFrame:CGRectMake(16,45,122,258)];  
  71.     leftdown.backgroundColor=[UIColor clearColor];  
  72.     [self.view addSubview:leftdown];  
  73.     [animationViewArray addObject:leftdown];  
  74.     [leftdown release];  
  75.       
  76.       
  77.     UIImageView *bianliImageButton=[[UIImageView alloc]initWithFrame:CGRectMake(5,164,122,91)];  
  78.     bianliImageButton.image=[UIImage imageNamed:@"note_bl.png"];  
  79.     [leftdown   addSubview:bianliImageButton];  
  80.     [bianliImageButton release];  
  81.       
  82.     UIImageView *bianlishenghuoImage=[[UIImageView alloc]initWithFrame:CGRectMake(60,0,10, 175)];  
  83.     bianlishenghuoImage.image=[UIImage imageNamed:@"rope_lang-.png"];  
  84.     [leftdown   addSubview:bianlishenghuoImage];  
  85.     [bianlishenghuoImage release];  
  86.       
  87.       
  88.     //帐单还款view设置  
  89.     leftup=[[UIImageView alloc]initWithFrame:CGRectMake(48,45,122,148)];  
  90.     leftup.backgroundColor=[UIColor clearColor];  
  91.     [self.view addSubview:leftup];  
  92.     [animationViewArray addObject:leftup];  
  93.     [leftup release];  
  94.       
  95.       
  96.     UIImageView *zhangdanImageButton=[[UIImageView alloc]initWithFrame:CGRectMake(1,56,125,91)];  
  97.     zhangdanImageButton.image=[UIImage imageNamed:@"note_cx.png"];  
  98.     [leftup addSubview:zhangdanImageButton];  
  99.     [zhangdanImageButton release];  
  100.       
  101.     UIImageView *zhangdanhaikuanImage=[[UIImageView alloc]initWithFrame:CGRectMake(60,0,10, 70)];  
  102.     zhangdanhaikuanImage.image=[UIImage imageNamed:@"rope.png"];  
  103.     [leftup   addSubview:zhangdanhaikuanImage];  
  104.     [zhangdanhaikuanImage release];  
  105.    
  106.      //用卡服务view设置  
  107.     rightDown=[[UIImageView alloc]initWithFrame:CGRectMake(160,45,122,251)];  
  108.     rightDown.backgroundColor=[UIColor clearColor];  
  109.     [self.view addSubview:rightDown];  
  110.      [animationViewArray addObject:rightDown];  
  111.     [rightDown release];  
  112.       
  113.   
  114.       
  115.     UIImageView *yongkaImageButton=[[UIImageView alloc]initWithFrame:CGRectMake(5,163,122,90)];  
  116.     yongkaImageButton.image=[UIImage imageNamed:@"note_zs_a.png"];  
  117.     [rightDown   addSubview:yongkaImageButton];  
  118.     [yongkaImageButton release];  
  119.       
  120.     UIImageView *fuwuImage=[[UIImageView alloc]initWithFrame:CGRectMake(60,0,10,175)];  
  121.     fuwuImage.image=[UIImage imageNamed:@"rope_lang-.png"];  
  122.     [rightDown   addSubview:fuwuImage];  
  123.     [fuwuImage release];  
  124.    
  125.       
  126.     //缤纷优惠view设置  
  127.     rightUp=[[UIImageView alloc]initWithFrame:CGRectMake(180,45,122,148)];  
  128.     rightUp.backgroundColor=[UIColor clearColor];  
  129.     [self.view addSubview:rightUp];  
  130.     [animationViewArray addObject:rightUp];  
  131.     [rightUp release];  
  132.       
  133.     UIImageView *bfImageButton=[[UIImageView alloc]initWithFrame:CGRectMake(2,60,125,88)];  
  134.     bfImageButton.image=[UIImage imageNamed:@"note_bf.png"];  
  135.     [rightUp  addSubview:bfImageButton];  
  136.     [bfImageButton release];  
  137.       
  138.     UIImageView *youhuiImage=[[UIImageView alloc]initWithFrame:CGRectMake(58,0,10, 70)];  
  139.     youhuiImage.image=[UIImage imageNamed:@"rope_lang-.png"];  
  140.     [rightUp  addSubview:youhuiImage];  
  141.     [youhuiImage release];  
  142.     
  143.     UIImageView *arrowImage=[[UIImageView alloc]initWithFrame:CGRectMake(0,44,320, 19)];  
  144.     arrowImage.image=[UIImage imageNamed:@"arrowall2.png"];  
  145.     [self.view   addSubview:arrowImage];  
  146.     [arrowImage release];  
  147.       
  148. }  
  149.    
  150.   
  151. -(void)doanimation{  
  152.       
  153.     NSArray* tempXPoint=[[NSArray alloc]initWithObjects:@"77",@"109",@"221",@"241",nil];  
  154.       
  155.     for (int i=0; i<[animationViewArray count]; i++){  
  156.           
  157.         [self doWiggle:[animationViewArray objectAtIndex:i]  startPoint:[tempXPoint objectAtIndex:i]];  
  158.     }  
  159.       
  160.     [tempXPoint release];  
  161. }  
  162.   
  163. -(void)doWiggle:(UIView *)touchView startPoint:(NSString *)string{  
  164.       
  165.     CALayer *touchedLayer = [touchView layer];  
  166.     float xpoint=[string floatValue];  
  167.     const CGFloat boneScale = 1;  
  168.     CATransform3D scale = CATransform3DMakeScale(boneScale , boneScale, 1);  
  169.       
  170.     touchedLayer.position = CGPointMake(xpoint,45);  
  171.     touchedLayer.anchorPoint = CGPointMake(0.5, 0.0);  
  172.     touchedLayer.bounds = CGRectMake(0,0,122,88);  
  173.     touchedLayer.transform = scale;  
  174.     CABasicAnimation* r1 = [CABasicAnimation animationWithKeyPath:@"transform"];  
  175.     CATransform3D rot1 = CATransform3DMakeRotation(0.5, 0,0,1);  
  176.     rot1 = CATransform3DConcat(rot1, touchedLayer.transform);  
  177.     r1.toValue = [NSValue valueWithCATransform3D:rot1];  
  178.     r1.autoreverses = YES;  
  179.     r1.repeatCount = NO;  
  180.     r1.duration = 0.5;  
  181.     r1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];  
  182.     [touchedLayer addAnimation:r1 forKey:nil];  
  183.    
  184. }  
  185.   
  186.   
  187.   
  188. #pragma mark -  
  189. #pragma mark UIAccelerometerDelegate  
  190.    
  191. -(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {  
  192.    
  193. //    NSLog(@"==%f,%f", acceleration.x,lastAccelerationX);  
  194. //    NSLog(@"%f",ABS(lastAccelerationX-acceleration.x));  
  195.       
  196.     if( ABS(lastAccelerationX-acceleration.x)>Accelerometer_Notice_Windage &&  
  197.        ABS(lastAccelerationY-acceleration.y)>Accelerometer_Notice_Windage &&  
  198.        ABS(lastAccelerationZ-acceleration.z)>Accelerometer_Notice_Windage )  
  199.     {  
  200.         NSLog(@"asdfasf");  
  201.         [self doanimation];  
  202.     }  
  203.     lastAccelerationX=acceleration.x;  
  204.     lastAccelerationY=acceleration.y;  
  205.     lastAccelerationZ=acceleration.z;  
  206.    
  207. }  
效果:


效果:


效果:



源代码下载

其他功能还在完善,敬请期待!

0 0
原创粉丝点击