您的位置:首页 > 产品设计 > UI/UE

UIImageView 与 CABasicAnimation 实现多层视图,带动画效果objec iOS

2013-05-31 17:13 302 查看
-(void)viewDidLoad
{
[superviewDidLoad];
inti;
intn=0;

UIScrollView*newscrollview=[[UIScrollViewalloc]init];
newscrollview.frame=CGRectMake(0,0,320,400);
[self.viewaddSubview:newscrollview];

newscrollview.backgroundColor=[UIColorbrownColor];
//button.backgroundColor=[UIColorredColor];
//button.tag=0;

//关联事件。。butttest

for(i=0;i<20;i++)
{
UIButton*button=[UIButtonbuttonWithType:UIButtonTypeCustom];
[newscrollviewaddSubview:button];
button.frame=CGRectMake(0,1+n,280,110);
button.backgroundColor=[UIColorredColor];
//与事件关联调用buttest方法
[buttonaddTarget:selfaction:@selector(butttest:)forControlEvents:UIControlEventTouchUpInside];

button.tag=i;
//在视图里加载imageview视图
UIImageView*uiimageview=[[UIImageViewalloc]init];
uiimageview.frame=CGRectMake(1,1+n,90,110);

UIImageView*uiimageview1=[[UIImageViewalloc]init];
UIImageView*uiimageview2=[[UIImageViewalloc]init];
uiimageview1.frame=CGRectMake(92,1+n,90,110);
uiimageview2.frame=CGRectMake(185,1+n,90,110);
[newscrollviewaddSubview:uiimageview1];
[newscrollviewaddSubview:uiimageview2];
uiimageview1.image=[UIImageimageNamed:@"three.png"];
uiimageview2.image=[UIImageimageNamed:@"four.png"];
[newscrollviewaddSubview:uiimageview];
//uiimageview.backgroundColor=[UIColoryellowColor];
uiimageview.image=[UIImageimageNamed:@"two.png"];

n=n+113;

}
[newscrollviewsetContentSize:CGSizeMake(320,2000)];
newscrollview.showsVerticalScrollIndicator=NO;
//Doanyadditionalsetupafterloadingtheviewfromitsnib.

}

-(IBAction)butttest:(id)sender
{
NSLog(@"nextaddabigbutton...\n");

staticintn=0;
if(maximgbutton==nil)
{
maximgbutton=[UIButtonbuttonWithType:UIButtonTypeCustom];
maximgbutton.frame=CGRectMake(0,0,320,400);
//maximgbutton.backgroundColor=[UIColorredColor];
[self.viewaddSubview:maximgbutton];

}

if(2==((UIButton*)sender).tag)
{
[maximgbuttonaddTarget:selfaction:@selector(removebutton:)forControlEvents:UIControlEventTouchUpInside];
NSLog(@"successful");
if(imageviewts==nil)
{
n++;

CABasicAnimation*theAnimation;
theAnimation=[CABasicAnimationanimationWithKeyPath:@"transform.translation.x"];

theAnimation.fromValue=[NSNumbernumberWithFloat:0.09];
//移动的幅度
theAnimation.toValue=[NSNumbernumberWithFloat:320];
//x,y轴缩小到0.1,Z轴不变
//theAnimation.toValue=[NSValuevalueWithCATransform3D:CATransform3DMakeScale(320,400,1.0)];
//持续时间
theAnimation.duration=2;
//重复次数
theAnimation.repeatCount=1;
theAnimation.removedOnCompletion=NO;

theAnimation.autoreverses=YES;

imageviewts=[[UIImageViewalloc]init];
imageviewts=[imageviewtsautorelease];
imageviewts.frame=CGRectMake(0,0,320,400);
imageviewts.image=[UIImageimageNamed:@"two.png"];
[self.viewaddSubview:imageviewts];

[imageviewts.layeraddAnimation:theAnimationforKey:nil];
NSLog(@"nnnnnn%i",n);

}
else
{
imageviewts.frame=CGRectMake(0,0,320,400);
imageviewts.image=[UIImageimageNamed:@"two.png"];
[self.viewaddSubview:imageviewts];

}
}
}

-(IBAction)removebutton:(id)sender
{
NSLog(@"remove....button\n");
staticintCalculator=0;
Calculator++;
if(Calculator%2!=0)
{
[imageviewtsremoveFromSuperview];
[maximgbuttonremoveFromSuperview];
maximgbutton=nil;
imageviewts=nil;
}
NSLog(@"Calculator%i",Calculator);

}


-(IBAction)removebutton:(id)sender
{
NSLog(@"remove....button\n");
staticintCalculator=0;
Calculator++;
if(Calculator%2!=0)
{
CABasicAnimation*theAnimation;
theAnimation=[CABasicAnimationanimationWithKeyPath:@"transform.translation.x"];

theAnimation.fromValue=[NSNumbernumberWithFloat:0];
//移动的幅度
theAnimation.toValue=[NSNumbernumberWithFloat:0];
//x,y轴缩小到0.1,Z轴不变
//theAnimation.toValue=[NSValuevalueWithCATransform3D:CATransform3DMakeScale(320,400,1.0)];
//持续时间
theAnimation.duration=2;
//重复次数
theAnimation.repeatCount=1;
//theAnimation.delegate=self;

//把imageviews这个视图加上theAnimation这个动画效果
[imageviewts.layeraddAnimation:theAnimationforKey:nil];
//开始加载动画效果
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:1];
[UIViewsetAnimationDelegate:self];
//view大小位置
imageviewts.frame=CGRectMake(-320,0,320,400);
//当动画停止的时候,调用hideAnimationStopped事件
[UIViewsetAnimationDidStopSelector:@selector(hideAnimationStopped)];
[UIViewcommitAnimations];

}
NSLog(@"Calculator%i",Calculator);

}




-(void)hideAnimationStopped
{
NSLog(@"imageViewStop");
[imageviewtsremoveFromSuperview];
[maximgbuttonremoveFromSuperview];
maximgbutton=nil;
imageviewts=nil;

}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: