【发布时间】:2015-12-22 13:46:26
【问题描述】:
我希望完全以编程方式执行以下操作,并拥有一些几乎可以做到的代码!
目标
两个主视图之间的转换,viewOne 和 viewTwo。 'viewOne' 是初始全屏视图。
-
使用不同方向和类型的幻灯片进行过渡
一个。专门使用左/右或上/下
b.能够选择方向(例如“向上”或“向下”)
观察到的代码响应
[Good]以下代码在视图之间成功转换
[问题]下面的代码只使用相同的转换,每次左->右!
核心代码
(下面的第二个块中发布的剩余代码段)
func pick_view(showFirst: Bool) {
let addedView = (showFirst) ? viewOne : viewTwo;
let removedView = (showFirst) ? viewTwo : viewOne;
print("picking a new view");
//let animationType = (showFirst) ? UIViewAnimationOptions.TransitionFlipFromLeft : .TransitionFlipFromRight; //flip L->R or R-> depending
addedView.alpha = 0;
self.view.addSubview(addedView);
print("old view off!");
UIView.animateWithDuration(0.5, delay: 0.5, options: .TransitionCurlUp, animations: { //.CurveLinear is an option too
print("new view added");
addedView.alpha = 1.0;
addedView.frame = self.view.frame;
}, completion: { (finished: Bool) -> Void in
print("old view removed");
removedView.frame = CGRectMake(self.view.frame.width, 0, 0, 0);
removedView.removeFromSuperview();
});
UIView.commitAnimations();
return;
}
剩余的代码段
回想一下,这是 100% 程序化的。因此,我觉得这里的代码非常有价值,能够做出响应:)
import UIKit
class ViewController: UIViewController {
var viewOne : UIView!;
var viewTwo : UIView!;
override func viewDidLoad() {
super.viewDidLoad();
gen_views();
pick_view(true);
return;
}
func gen_views() {
viewOne = UIView();
viewOne.backgroundColor = UIColor.redColor();
viewOne.frame = self.view.frame;
addSecondUIView(viewOne, dispStr: "4:30 PM");
addSubviewButton(viewOne, return_msg: "Launch View #2", action_fcn: "press_launch:");
viewTwo = UIView();
viewTwo.backgroundColor = UIColor.blueColor();
viewTwo.frame = self.view.frame;
addSecondUIView(viewTwo, dispStr: "8:00 PM");
addSubviewButton(viewTwo, return_msg: "Return from View #2", action_fcn: "press_return:");
viewTwo.frame = CGRectMake(self.view.frame.width, 0, 0, 0); //init off-screen
self.view.addSubview(viewOne); //add em both!
self.view.addSubview(viewTwo);
return;
}
func pick_view(showFirst: Bool) {
//see above :)
}
func addSecondUIView(aView: UIView, dispStr : String) {
let anotherView : UIView = UIView(frame: CGRect(x: 20, y:60, width: 100, height: 50));
anotherView.backgroundColor = UIColor.grayColor();
anotherView.layer.cornerRadius = 15;
let someLabel : UILabel = UILabel(frame: CGRect(x:5, y: 0, width: anotherView.frame.width, height: anotherView.frame.height));
someLabel.font = UIFont(name: "HelveticaNeue", size: 23);
someLabel.text = dispStr;
someLabel.textColor = UIColor.whiteColor();
someLabel.textAlignment = NSTextAlignment.Center;
anotherView.addSubview(someLabel);
aView.addSubview(anotherView);
return;
}
func addSubviewButton(aView: UIView, return_msg: String, action_fcn : Selector) {
let newButton : UIButton = UIButton(type: UIButtonType.RoundedRect);
newButton.translatesAutoresizingMaskIntoConstraints = true; //must be true for center to work
newButton.setTitle(return_msg, forState: UIControlState.Normal);
newButton.backgroundColor = UIColor.whiteColor();
//newButton.frame = CGRectMake(0, 0, 144, 30); // or just use sizeToFit(), it's easier!
newButton.sizeToFit();
newButton.center = CGPointMake((UIScreen.mainScreen().bounds.width)/2, 250); //must call after it's sized or won't work!
//actions
newButton.addTarget(self, action: action_fcn, forControlEvents: .TouchUpInside);
//add!
aView.addSubview(newButton);
return;
}
func press_launch(sender: UIButton!) {
print("\(sender.titleLabel!.text!) was pressed and press_launch called");
pick_view(false);
return;
}
func press_return(sender: UIButton!) {
print("\(sender.titleLabel!.text!) was pressed and press_return called");
pick_view(true);
return;
}
override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning(); }
}
【问题讨论】:
-
您的问题到底是什么?您尝试过什么来实现不同的滑动方向?您的代码中只有一个“问题”——动画选项(即
TransitionCurlUp或TransitionFlipFromLeft)仅在使用transitionWithView:...ortransitionFromView:...方法时使用。 -
我从 'UIView.animateWithDuration'() 更改为 'UIView.transitionWithView()',但仍然无法修改!代码现在做了一个不同的动画,从屏幕外向右扩展新视图。另请注意,我尝试过差异动画 - 取消注释“animationType”并尝试:)!
-
对于这个问题,如你所问,请参阅“观察到的代码响应 -> [问题]”... :)
标签: ios swift uiview subview animatewithduration