UIViewAnimationOptions.TransitionFlipFromLeft第二次不显示UIView

时间:2016-07-12 08:58:40

标签: ios objective-c swift swift2 uiviewanimation

我正在尝试翻转两个UIViews。我试图以编程方式翻转UIView并且它完美无缺。但是,当我尝试翻转UIView我在storyboard中创建的UiViews时,它不起作用,第一次翻转UIView但是第二次翻转空白Debug view Hierarchy?任何人都知道我的代码中有任何错误吗?

enter image description here 在此图片中,左上角Debug view Hierarchy图片位于动画按钮之前,左下方UIView图片位于动画图片之后。

当我第二次为class ViewController: UIViewController { @IBOutlet var container: UIView! @IBOutlet var blueSquare : UIView! @IBOutlet var redSquare : UIView! override func viewDidLoad() { super.viewDidLoad() } @IBAction func animateButtonTapped(sender: AnyObject) { // create a 'tuple' (a pair or more of objects assigned to a single variable) var views : (frontView: UIView, backView: UIView) if ((self.redSquare.superview) != nil) { views = (frontView: self.redSquare, backView: self.blueSquare) } else { views = (frontView: self.blueSquare, backView: self.redSquare) } // set a transition style let transitionOptions = UIViewAnimationOptions.TransitionFlipFromLeft // with no animation block, and a completion block set to 'nil' this makes a single line of code UIView.transitionFromView(views.frontView, toView: views.backView, duration: 1.0, options: transitionOptions, completion: nil) } } 动画时,它会像下面的图片一样翻转。 enter image description here

let container = UIView()
let redSquare = UIView()
let blueSquare = UIView()

override func viewDidLoad() {
    super.viewDidLoad()

    // set container frame and add to the screen
    self.container.frame = CGRect(x: 60, y: 60, width: 200, height: 200)
    self.view.addSubview(container)

    // set red square frame up
    // we want the blue square to have the same position as redSquare 
    // so lets just reuse blueSquare.frame
    self.redSquare.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
    self.blueSquare.frame = redSquare.frame

    // set background colors
    self.redSquare.backgroundColor = UIColor.redColor()
    self.blueSquare.backgroundColor = UIColor.blueColor()

    // for now just add the redSquare
    // we'll add blueSquare as part of the transition animation 
    self.container.addSubview(self.redSquare)   
}

@IBAction func animateButtonTapped(sender: AnyObject) {

    // create a 'tuple' (a pair or more of objects assigned to a single variable)
    var views : (frontView: UIView, backView: UIView)

    if((self.redSquare.superview) != nil){
        views = (frontView: self.redSquare, backView: self.blueSquare)
    }
    else {
        views = (frontView: self.blueSquare, backView: self.redSquare)
    }

    // set a transition style
    let transitionOptions = UIViewAnimationOptions.TransitionFlipFromLeft

    // with no animation block, and a completion block set to 'nil' this makes a single line of code  
    UIView.transitionFromView(views.frontView, toView: views.backView, duration: 1.0, options: transitionOptions, completion: nil)

}
  

编程   该代码完美无缺。

var check = true
 @IBAction func animateButtonTapped(sender: AnyObject) {
    // create a 'tuple' (a pair or more of objects assigned to a single variable)
    var views : (frontView: UIView, backView: UIView)

    if (check == true) {

        views = (frontView: self.redSquare, backView: self.blueSquare)
        check = false
    }
    else {
        views = (frontView: self.blueSquare, backView: self.redSquare)
        check = true
    }

    // set a transition style
    let transitionOptions : UIViewAnimationOptions = [UIViewAnimationOptions.TransitionFlipFromLeft, UIViewAnimationOptions.ShowHideTransitionViews]
    // with no animation block, and a completion block set to 'nil' this makes a single line of code
    UIView.transitionFromView(views.frontView, toView: views.backView, duration: 1.0, options: transitionOptions, completion: nil)

}
  

更新

-(void)flashOn {
Class captureDeviceClass = NSClassFromString(@"AVCaptureDevice");
if (captureDeviceClass != nil) {
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    [device lockForConfiguration:nil];

    if ([device hasFlash]) {
        if ([device flashMode] == AVCaptureFlashModeOff) {
            [device setFlashMode:AVCaptureFlashModeOn];
        }
    }

    if ([device hasTorch]) {
        if ([device torchMode] == AVCaptureTorchModeOff) {
            [device setTorchMode:AVCaptureTorchModeOn];
        }
    }

    [device unlockForConfiguration];
  }
}

2 个答案:

答案 0 :(得分:2)

transitionFromView的默认行为会在动画后删除视图。

let transitionOptions: UIViewAnimationOptions = [.TransitionFlipFromLeft, .ShowHideTransitionViews]

来自文档:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/swift/struct/c:@E@UIViewAnimationOptions

  
      
  • ShowHideTransitionViews
  •   
     

如果存在,此键会导致在执行视图转换时隐藏或显示视图(而不是删除或添加)。使用此密钥时,两个视图必须已存在于父视图的层次结构中。如果此键不存在,则转换中的to-view将添加到父视图的子视图列表中,并从中移除视图。

答案 1 :(得分:0)

啊在故事板中我看到你将两个视图添加到容器中但在代码中你只添加了redsquare。也许删除bluesquare不在故事板中的容器内?