2012-03-04 101 views
0

我是一位新的ios開發人員。我想創建一個ios應用程序,我想添加兩個視圖。首先當應用程序啓動時,第一個視圖將顯示。將會有一個按鈕。當點擊按鈕時,第二個視圖從左到右顯示在屏幕上,而不是全屏,但在屏幕上顯示一半。 我該怎麼做。 在此先感謝。ui查看從左至右的翻譯

+0

您是否知道viewcontrollers和xib以及如何爲按鈕創建動作?您目前正在使用哪種XCode以及您希望爲其創建此應用程序的iOS版本。 – Ravin 2012-03-04 19:41:06

回答

1

在UIView的animateWithDuration包裝的幫助下,它實際上很容易。如果你對塊不熟悉,這也是一個很好的學習機會。

首先,聲明在.H 2個的UIView對象,並定義要掛鉤到該按鈕的方法:

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

@end 

現在在.M,定義你的動作(注意它的返回類型更改無效,但它的簽名保持不變):

#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中的按鈕。