uiview从左到右过渡

时间:2012-03-04 19:26:14

标签: iphone uiview

我是一名新的ios开发人员。我想创建一个ios应用程序,我想添加两个视图。首先,当应用程序启动时,第一个视图将显示。会有一个按钮。点击按钮后,第二个视图将从左到右显示在屏幕上,而不是全屏,而是屏幕的一半。 我怎样才能做到这一点。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

在UIView的animateWithDuration包装器的帮助下,它实际上非常简单。如果您不熟悉积木,这也是一个很好的学习机会。

首先,在.h中声明两个UIView对象,并定义要连接到按钮的方法:

@interface Example : UIViewController
{
    UIView *_view1;
    UIView *_view2;
}
@property (nonatomic, retain) UIView *view1;
@property (nonatomic, retain) UIView *view2;
-(IBAction)animateViews:(id)sender;

@end

现在在.m中,定义你的动作(注意它的返回类型变为void,但它的签名保持不变):

#import "Example.h"

@implementation Example
@synthesize view1 = _view1;
@synthesize view2 = _view2;

-(void)viewDidLoad {
    //alloc and init views, add to view
    self.view1 = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view2 = [[UIView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width, 0, self.view.bounds.size.width/2, self.view.bounds.size.height)];
    //Set the background color so we can actually see the views, the first will be grey, the second, black.
    [self.view1 setBackgroundColor:[UIColor grayColor]];
    [self.view2 setBackgroundColor:[UIColor darkTextColor]];
    //add subview to main view
    [self.view addSubview:self.view1];
    [self.view addSubview:self.view2];

    [super viewDidLoad];
}

-(void)animateViews:(id)sender {

         /*I absolutely love the UIView animation block, 
 it's possibly the most helpful thing in terms of animation apple could have made.  
 Any property changed inside this block (in this case, the frame property), 
 is automatically animated for the duration you specify.  
 It's even got a built in completion block!  So cool.*/

    [UIView animateWithDuration:2.5 animations:^{
        [self.view1 setFrame:CGRectMake(0, 0, self.view.bounds.size.width/2, self.view.bounds.size.height)];
        [self.view2 setFrame:CGRectMake(self.view.bounds.size.width/2, 0, self.view.bounds.size.width/2, self.view.bounds.size.height)];
     }];

}

@end

这应该为第一个视图的框架设置动画以占据屏幕的一半,然后设置第二个视图的动画以便进入并占据另一半。确保在运行之前将IBAction连接到XIB中的按钮。