0

我仍然是 xcode 的初学者。这是我的问题..我有一个以编程方式制作的滚动视图,其中有一个以编程方式制作的uilabel。我已将 ssrollview 放置在视图控制器的边缘,使其不可见。我想动画它滑动并在按下按钮时出现在视图控制器中。我该怎么做呢?当我只为滚动视图设置动画时,它工作得很好,但是当 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];

}

4

1 回答 1

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

}
于 2013-04-23T11:53:17.313 回答