2017-03-15 11 views
0

私はそこにボタンも、ユーザーがボタンを押したとき、それは最初のビューおよびその逆怒鳴るビューにそれを取るような方法で、単一のViewControllerに2つのフルスクリーンUIViewsを持つ方法を探していますあなたはそのためにscrollviewを使用することができます1つのUIVIewControllerに2つのフルスクリーンUIViewを垂直に配置しますか?

enter image description here

+0

ビュー間を移動するときに視覚効果を欲しいですか? uiscrollviewは、一度に収まるより多くのビューを表示する一般的な方法です。これにより、(ユーザーのための)スクロールがオフになり、ボタンが押されたときにアプリケーションがスクロール(フルページの上下に)を実行するようにすることができます。 – danh

+0

小さなスクロール効果があまりにも好きではないでしょう –

答えて

1

:...シンプルな言葉では、私は単一のUIViewController

に縦に2つのフルスクリーンUIViewsを置きたい私はこのような何かを探していますシナリオ。サブビューとして二つのビューはscrollviewに追加し、ユーザーがボタン(または私の例ではセグメント化されたコントロール)をタップしたときにscrollviewのcontentoffsetを更新:

var scrollView: UIScrollView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let scrollView = UIScrollView() 
    scrollView.translatesAutoresizingMaskIntoConstraints = false 
    scrollView.scrollEnabled = false 
    view.addSubview(scrollView) 
    self.scrollView = scrollView 

    let firstView = UIView() 
    firstView.translatesAutoresizingMaskIntoConstraints = false 
    firstView.backgroundColor = UIColor.lightGrayColor() 
    scrollView.addSubview(firstView) 

    let secondView = UIView() 
    secondView.translatesAutoresizingMaskIntoConstraints = false 
    secondView.backgroundColor = UIColor.darkGrayColor() 
    scrollView.addSubview(secondView) 

    NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[sv]|", options: [], metrics: nil, views: ["sv": scrollView])) 
    NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[sv]|", options: [], metrics: nil, views: ["sv": scrollView])) 

    NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[fv]|", options: [], metrics: nil, views: ["fv": firstView])) 
    NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[fv][sv]|", options: [.AlignAllLeading, .AlignAllTrailing], metrics: nil, views: ["fv": firstView, "sv": secondView])) 

    firstView.widthAnchor.constraintEqualToAnchor(scrollView.widthAnchor).active = true 
    firstView.heightAnchor.constraintEqualToAnchor(scrollView.heightAnchor).active = true 

    firstView.widthAnchor.constraintEqualToAnchor(secondView.widthAnchor).active = true 
    firstView.heightAnchor.constraintEqualToAnchor(secondView.heightAnchor).active = true 

    let segmentedControl = UISegmentedControl(items: ["firstView", "secondView"]) 
    segmentedControl.translatesAutoresizingMaskIntoConstraints = false 
    view.addSubview(segmentedControl) 
    segmentedControl.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true 
    NSLayoutConstraint.activateConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[sc]-20-|", options: [], metrics: nil, views: ["sc": segmentedControl])) 

    segmentedControl.selectedSegmentIndex = 0 
    segmentedControl.addTarget(self, action: #selector(segmentedControlValueChanged(_:)), forControlEvents: .ValueChanged) 
} 

func segmentedControlValueChanged(segmentedControl: UISegmentedControl) { 
    scrollView.setContentOffset(CGPointMake(0, CGFloat(segmentedControl.selectedSegmentIndex) * scrollView.frame.height), animated: true) 
} 

は、私はあなたが右だ願っています。何かが不明な場合はお気軽にお問い合わせください!

UPDATE(OBJ-C)

@interface ViewController() 

@property (strong, nonatomic) UIScrollView *scrollView; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIScrollView *scrollView = [UIScrollView new]; 
    scrollView.translatesAutoresizingMaskIntoConstraints = NO; 
    scrollView.scrollEnabled = NO; 
    [self.view addSubview:scrollView]; 
    self.scrollView = scrollView; 

    UIView *firstView = [UIView new]; 
    firstView.translatesAutoresizingMaskIntoConstraints = NO; 
    firstView.backgroundColor = [UIColor lightGrayColor]; 
    [scrollView addSubview:firstView]; 

    UIView *secondView = [UIView new]; 
    secondView.translatesAutoresizingMaskIntoConstraints = NO; 
    secondView.backgroundColor = [UIColor darkGrayColor]; 
    [scrollView addSubview:secondView]; 

    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[sv]|" options:0 metrics:nil views:@{@"sv": scrollView}]]; 
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[sv]|" options:0 metrics:nil views:@{@"sv": scrollView}]]; 

    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[fv]|" options:0 metrics:nil views:@{@"fv": firstView}]]; 
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[fv][sv]|" options:NSLayoutFormatAlignAllLeading|NSLayoutFormatAlignAllTrailing metrics:nil views:@{@"fv": firstView, @"sv": secondView}]]; 

    [firstView.widthAnchor constraintEqualToAnchor:scrollView.widthAnchor].active = YES; 
    [firstView.heightAnchor constraintEqualToAnchor:scrollView.heightAnchor].active = YES; 

    [firstView.widthAnchor constraintEqualToAnchor:secondView.widthAnchor].active = YES; 
    [firstView.heightAnchor constraintEqualToAnchor:secondView.heightAnchor].active = YES; 

    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"firstView", @"secondView"]]; 
    segmentedControl.translatesAutoresizingMaskIntoConstraints = NO; 
    [self.view addSubview:segmentedControl]; 
    [segmentedControl.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES; 
    [NSLayoutConstraint activateConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[sc]-20-|" options:0 metrics:nil views:@{@"sc": segmentedControl}]]; 

    segmentedControl.selectedSegmentIndex = 0; 
    [segmentedControl addTarget:self action:@selector(segmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged]; 
} 

- (void)segmentedControlValueChanged:(UISegmentedControl *)segmentedControl { 
    [self.scrollView setContentOffset:CGPointMake(0, segmentedControl.selectedSegmentIndex * CGRectGetHeight(self.scrollView.frame)) animated:YES]; 
} 

@end 
+0

ありがとうAndre、任意のチャンスでObj-Cの同じコードベースを持っていますか? –

+0

@NavNav私は客観的なCバージョンで私の答えを更新しました... –

+0

それを感謝します:) –

1

あなたはUITableView 2の細胞を使用することができ、画面全体を占有して、各セルには、(動的に高さを設定する方法を調べます)。

あなたは明らか独自のクラス&のロジックを持っているでしょう2個のカスタムセル、各ビューのための1つを、持っている可能性があります。働くだろう

は、ビューの一部のみが表示され、または手動でスクロールを制御することを避けるために、ビューの上にスナップのように、様々なscrollviewの利点を使用することができると思います。あなたが好きなだけかもしれない:

また、あなたはビューの数を変更する必要がある場合には、将来の証拠になります!

彼らはscrollviewにいるので、彼らはまた、画面のサイズよりも小さく/大きくてもよいです。

1

その非常に簡単本当にあなたがautoLayoutを使用して提供。 UIViewをストーリーボードにドラッグアンドドロップし、先頭、末尾、下端、上端をスーパービューに追加します。これをビュー1と呼ぶことができます。別のUIViewビュー1と同じ階層レベルでストーリーボードにドラッグアンドドロップすると、この新しいビューをビュー2と呼ぶことができます。今すぐ設定することがあなたのVIEW2用サイズインスペクタでオフセットyはあなたのビューの階層構造で今すぐ800

を言うことができます、制御 + VIEW1とドラッグをクリックしてVIEW2上にドロップすると、シフトを押してくださいを選択し、先頭、末尾、垂直スペーシングおよび等高さの制約を選択します。すべての定数が0に設定されていることを確認します(コンテンツに基づいて高さを推測し、フルスクリーンの高さと等しくないようにするには、等高さ制約の優先順位を250に設定します)。

画面は次のようになります。

enter image description here

今すぐあなたのVIEW1を選択し、トップの制約を選択してください。スーパービューに添付されたもの。この制約のコンセントをUIViewControllerクラスに作成し、viewOneTopSpaceConstraintとします。

今あなたがしなければならないすべてはちょうどトグルボタンのIBActionの内側に、次のとおりです。

if viewOneTopSpaceConstraint.constant == 0{ 

    viewOneTopSpaceConstraint.constant = -UIScreen.main.bounds.size.height 
}else{ 

    viewOneTopSpaceConstraint.constant = 0 
} 

UIView.animate(withDuration: 0.2, animations: { 

    self.view.layoutIfNeeded() 

} 
+0

ありがとうございました! –

1

一つの最後のオプション、私が読んだすべての答えは優れているものの、ユーザーに感じているものを提供することですビューはオフスクリーンで、一方はオンで、基本的にすべてをスーパービューに固定します。ここに私のセットアップとCATransitionsの使用があります。 Setup

マスターコンテナはメインビューに固定されていますが、コントロールがそこに移動する予定がある場合は、上部にスペースを追加できます。これは、両方ともmasterContainerに固定されている2つのビューを含んでいます。どちらにも変更を呼び出すボタンがあります。ストーリーボードビュー2にプリセットして、アプリの起動時に非表示にする。ここにコントローラがあります。ビュー1のボタンを押すと、ビュー2が下から上にスライドします。 view2を押すと、view1はview2を元に戻します。ここにコントローラと必要なコードがあります。

import UIKit 

class CATransitionDemoViewController: UIViewController { 

    @IBOutlet weak var masterContainer: UIView! 
    @IBOutlet weak var view2: UIView! 
    @IBOutlet weak var view1: UIView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
     view2.isHidden = true 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    @IBAction func viewButton1DidPress(_ sender: Any) { 
      let transition = CATransition() 
      transition.duration = 0.4 
      transition.type = kCATransitionMoveIn 
      transition.subtype = kCATransitionFromTop 
      self.masterContainer.layer.add(transition, forKey: nil) 
      view1.isHidden = !view1.isHidden 
      view2.isHidden = !view2.isHidden 
    } 

    @IBAction func viewButton2DidPress(_ sender: Any) { 
      let transition = CATransition() 
      transition.duration = 0.4 
      transition.type = kCATransitionPush 
      transition.subtype = kCATransitionFromBottom 
      self.masterContainer.layer.add(transition, forKey: nil) 
      view1.isHidden = !view1.isHidden 
      view2.isHidden = !view2.isHidden 
    } 

ORのObjective Cで

#import "ViewController.h" 

@interface ViewController() 
@property (weak, nonatomic) IBOutlet UIView *masterContainer; 
@property (weak, nonatomic) IBOutlet UIView *view1; 
@property (weak, nonatomic) IBOutlet UIView *view2; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    [_view2 setHidden:YES]; 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
- (IBAction)buttonView1DidPress:(id)sender { 
    CATransition *transition = [[CATransition alloc]init]; 
    transition.duration = 0.5; 
    //or for some real fun uncomment the next line and comment the two lines out after that one 
    //transition.type = @"suckEffect"; 
    transition.type = kCATransitionPush; 
    transition.subtype = kCATransitionFromTop; 
    [_masterContainer.layer addAnimation:transition forKey:nil]; 
    [_view1 setHidden:!_view1.hidden]; 
    [_view2 setHidden:!_view2.hidden]; 
} 

- (IBAction)buttonView2DidPress:(id)sender { 
    CATransition *transition = [[CATransition alloc]init]; 
    transition.duration = 0.5; 
    transition.type = kCATransitionPush; 
    transition.subtype = kCATransitionFromBottom; 
    [_masterContainer.layer addAnimation:transition forKey:nil]; 
    [_view1 setHidden:!_view1.hidden]; 
    [_view2 setHidden:!_view2.hidden]; 
} 

@end 

この方法の利点は、あなたがストーリーボードすべてで作業しているときscrollviewの設定なしに簡単にアクセスできるということです。さらに、CATransitionsにはさらに多くのオプションがあります。

+0

obj-cでソースコードを共有することは可能ですか? –

+0

完了。 ......... – agibson007

+0

たくさんの愛人!ありがとう –

関連する問題