ios cabasicanimation为什么动画没效果

 我来答
可以叫我表哥
推荐于2016-08-08 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
采纳数:25897 获赞数:1464984
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。

向TA提问 私信TA
展开全部
- (void)viewDidLoad
{
[super viewDidLoad];
int i;
int n = 0;

UIScrollView *newscrollview = [[UIScrollView alloc]init];
newscrollview.frame = CGRectMake(0, 0, 320, 400);
[self.view addSubview:newscrollview];

newscrollview.backgroundColor = [UIColor brownColor];
//button.backgroundColor = [UIColor redColor];
//button.tag = 0;

//关联事件。。butttest

for(i = 0; i<20; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[newscrollview addSubview:button];
button.frame = CGRectMake(0, 1+n, 280, 110);
button.backgroundColor = [UIColor redColor];
//与事件关联 调用buttest方法
[button addTarget:self action:@selector(butttest:) forControlEvents:UIControlEventTouchUpInside];

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

UIImageView *uiimageview1 = [[UIImageView alloc]init];
UIImageView *uiimageview2 = [[UIImageView alloc]init];
uiimageview1.frame = CGRectMake(92, 1+n, 90, 110);
uiimageview2.frame = CGRectMake(185, 1+n, 90, 110);
[newscrollview addSubview:uiimageview1];
[newscrollview addSubview:uiimageview2];
uiimageview1.image = [UIImage imageNamed:@"three.png"];
uiimageview2.image = [UIImage imageNamed:@"four.png"];
[newscrollview addSubview:uiimageview];
//uiimageview.backgroundColor = [UIColor yellowColor];
uiimageview.image = [UIImage imageNamed:@"two.png"];

n = n + 113;

}
[newscrollview setContentSize:CGSizeMake(320, 2000)];
newscrollview.showsVerticalScrollIndicator = NO;
// Do any additional setup after loading the view from its nib.

}

-(IBAction)butttest:(id)sender
{
NSLog(@"next add a big button ...\n");

static int n=0 ;
if(maximgbutton == nil)
{
maximgbutton = [UIButton buttonWithType:UIButtonTypeCustom];
maximgbutton.frame = CGRectMake(0, 0, 320, 400);
//maximgbutton.backgroundColor = [UIColor redColor];
[self.view addSubview:maximgbutton];

}

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

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

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

theAnimation.autoreverses = YES;

imageviewts = [[UIImageView alloc]init];
imageviewts = [imageviewts autorelease];
imageviewts.frame = CGRectMake(0, 0, 320, 400);
imageviewts.image = [UIImage imageNamed:@"two.png"];
[self.view addSubview:imageviewts];

[imageviewts.layer addAnimation:theAnimation forKey:nil];
NSLog(@"nnnnnn%i",n);

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

}
}
}

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

}

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

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

//把imageviews这个视图加上theAnimation这个动画效果
[imageviewts.layer addAnimation:theAnimation forKey:nil];
//开始加载动画效果
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelegate:self];
//view 大小 位置
imageviewts.frame = CGRectMake(-320, 0, 320, 400);
//当动画停止的时候,调用hideAnimationStopped事件
[UIView setAnimationDidStopSelector:@selector(hideAnimationStopped)];
[UIView commitAnimations];

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

}

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

}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式