UISegmentedControl请查看动画代码审查

时间:2014-08-25 09:39:10

标签: ios animation swift uisegmentedcontrol

我只是在Swift中尝试一下,只是使用UISegmentedControl制作我的第一个动画,在视图之间切换,你能帮我做一些代码审查,知道我在正确的轨道上吗?

import UIKit;

class ViewLikes:UIViewController {

var innerViews:[UIViewController] = [UIViewController]()
var currentIndex:Int = 0;
var centerView:UIViewController = UIViewController()

override func viewDidLoad()  {
    super.viewDidLoad()
    title = "some title";

    var view1:UIViewController = UIViewController()
    var lab1:UILabel = UILabel(frame: CGRectMake(0, 10, 200, 21))
    lab1.center = CGPointMake(160, 284)
    lab1.textAlignment = NSTextAlignment.Center
    lab1.text = "I'am a label1"
    view1.view.addSubview(lab1)

    var view2:UIViewController = UIViewController()
    var lab2:UILabel = UILabel(frame: CGRectMake(0, 10, 200, 21))
    lab2.center = CGPointMake(160, 284)
    lab2.textAlignment = NSTextAlignment.Center
    lab2.text = "I'am a label2"
    view2.view.addSubview(lab2)

    var view3:UIViewController = UIViewController()
    var lab3:UILabel = UILabel(frame: CGRectMake(0, 10, 200, 21))
    lab3.center = CGPointMake(160, 284)
    lab3.textAlignment = NSTextAlignment.Center
    lab3.text = "I'am a label3"
    view3.view.addSubview(lab3)

    innerViews.append(view1)
    innerViews.append(view2)
    innerViews.append(view3)

    var segmentControl:UISegmentedControl = UISegmentedControl(items:["blah", "blah1", "blah2"]);
    segmentControl.selectedSegmentIndex = currentIndex;
    segmentControl.addTarget(self, action:"segmentSwitch:" , forControlEvents:UIControlEvents.ValueChanged  )

    segmentControl.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.centerView.view.setTranslatesAutoresizingMaskIntoConstraints(false)

    self.view.addSubview(segmentControl)
    self.view.addSubview(self.centerView.view)

    var currentView = self.innerViews[0]
    currentView.viewWillAppear(false)
    self.centerView.view.addSubview(currentView.view)
    currentView.viewDidAppear(false)


    //Set layout
    var viewsDict = Dictionary <String, UIView>()
    viewsDict["segment"] = segmentControl;
    viewsDict["center"] = self.centerView.view;

    self.view.addConstraint(NSLayoutConstraint(item: segmentControl,
                                attribute: NSLayoutAttribute.Top,
                                relatedBy: NSLayoutRelation.Equal,
                                toItem: self.topLayoutGuide,
                                attribute: NSLayoutAttribute.Bottom,
                                multiplier: 1, constant: 10))


    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[segment]-|",
                                options: NSLayoutFormatOptions(0),
                                metrics: nil,
                                views: viewsDict))


    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[center]-0-|",
        options: NSLayoutFormatOptions(0),
        metrics: nil,
        views: viewsDict))

    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[segment]-[center]-0-|",
        options: NSLayoutFormatOptions(0),
        metrics: nil,
        views: viewsDict))

}

func segmentSwitch(control:UISegmentedControl) {
    selectView(control.selectedSegmentIndex)
}

func selectView(index:Int) {
    var currentView = self.innerViews[self.currentIndex]

    var nextView = self.innerViews[index]
    nextView.viewWillAppear(false)
    self.centerView.view.addSubview(nextView.view)
    nextView.viewDidAppear(false)

    if(index > currentIndex) {
        nextView.view.frame.origin.x = nextView.view.frame.width;
    } else {
        nextView.view.frame.origin.x -= nextView.view.frame.width;
    }

    UIView.animateWithDuration(0.8,
        animations: {
            var currentFrame = currentView.view.frame;
            var nextFrame = nextView.view.frame;

            if(index > self.currentIndex) {
                currentFrame.origin.x -= currentFrame.size.width;
            } else {
                currentFrame.origin.x = currentFrame.size.width;
            }
            nextFrame.origin.x = 0.0;

            currentView.view.frame = currentFrame
            nextView.view.frame = nextFrame
        }, completion: {finished in
            currentView.viewWillDisappear(false)
            currentView.view.removeFromSuperview()
            currentView.viewDidDisappear(false)
        }
    );



    self.currentIndex = index

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    for innerView in self.innerViews {
        innerView.didReceiveMemoryWarning();
    }
}
}

0 个答案:

没有答案