2012-03-04 21 views
0

私は新しいios開発者です。私は2つのビューを追加したいios appを作成したいと思います。最初にappを起動すると、最初のビューが表示されます。ボタンがあります。タップ・オン・ボタンを押すと、2番目のビューが画面に左から右に表示され、フル・スクリーンではなく画面の半分に表示されます。 どうすればいいですか? ありがとうございます。uiビューの左から右への変換

+0

あなたはviewcontrollerとxibを認識していますか、ボタンのアクションを作成する方法はありますか?現在使用しているXCodeと、このアプリケーションを作成するiOSバージョン。 – Ravin

答えて

1

実際には、UIViewのanimateWithDurationラッパーの助けを借りて非常に簡単です。あなたがブロックに慣れていない場合でも、これは優れた学習機会です。

まず、.hの二つのUIViewオブジェクトを宣言し、あなたがボタンにフックアップするメソッドを定義します.Mで今

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

@end 

を、(あなたのアクションを定義し、それはへのリターンタイプの変更です気づきます)のボイドが、それは署名が同じままだ:

#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 

これは、画面の半分を取るために、最初のビューのフレームをアニメーション化して、ソートの中に飛ぶと他を取るために2番目のビューをアニメーション化すべきですハーフ。あなたはそれを実行する前に、そのIBActionをXIBのボタンに接続してください。

関連する問題