如何为包含子视图的视图设置动画

时间:2013-04-22 16:31:46

标签: core-animation xcode4.5 uiviewanimation addsubview

我还是xcode的初学者。这是我的问题..我有一个滚动视图,在程序上有一个uilabel在其中制作。我已将ssrollview放在视图控制器的边缘,以便它不可见。我想动画它滑动,并在按下按钮时出现在viewcontroller中。我该怎么做呢?当我只在动画滚动视图时,它工作得非常好,但是当uilabel被置于其中时,我无法为滚动视图设置动画..这是我使用过的。  以下块来自按钮动作。[注意:我使用过quartzcore框架]

    -(IBAction)slideback:(id)sender

{

viol=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 44.8, 320, 420)];
viol.contentSize=CGSizeMake(320, 900);
viol.showsVerticalScrollIndicator=YES;
viol.backgroundColor=[UIColor grayColor];
    viol.delegate=self;



CATransition *animation = [CATransition animation];

[animation setDuration:1];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

[[viol layer] addAnimation:animation forKey:@"SlideView"];

l1=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 700)];
l1.backgroundColor=[UIColor redColor];
l1.font=[UIFont boldSystemFontOfSize:16];
[l1 setNumberOfLines:0];
[l1 sizeToFit];
[viol addSubview:l1];

[self.view addSubview:viol];

}

1 个答案:

答案 0 :(得分:0)

代码很好。一旦你将文本设置为UILabel,它就可以使用了 [l1 sizeToFit];

sizeToFit调整大小并移动视图,使其包含其子视图。由于您在UILabel中没有文本,因此其宽度和高度将为(0,0)

-(IBAction)slideback:(id)sender {
  viol=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 44.8, 320, 420)];
viol.contentSize=CGSizeMake(320, 900);
viol.showsVerticalScrollIndicator=YES;
viol.backgroundColor=[UIColor grayColor];
viol.delegate=self;

[self.view addSubview:viol];


l1=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 700)];
l1.backgroundColor=[UIColor redColor];
l1.font=[UIFont boldSystemFontOfSize:16];
[l1 setNumberOfLines:0];
//    [l1 sizeToFit];
[viol addSubview:l1];

CATransition *animation = [CATransition animation];
[animation setDuration:2];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];


[[l1 layer] addAnimation:animation forKey:@"SlideView"];

}