2017-07-19 84 views
0

VideoUIViewAnimationTransition翻轉的UIImageView

我在底部拍了拍我的功能UIView.transitions工作第一次,但再翻回正面,第二次我else語句崩潰每次。獲取錯誤消息「致命錯誤:在解包可選值時意外發現零」。希望每次都能成功翻轉前面和後面翻轉:)先謝謝你了。 ;)另外,我在我的故事板中添加了ImageOne & ImageTwo視圖!

import UIKit 
import Foundation 
import Firebase 
import FirebaseDatabase 

class ViewController: UIViewController { 

@IBOutlet weak var mainScrollView: UIScrollView! 
@IBOutlet weak var imageOne: UIImageView! 
@IBOutlet weak var imageTwo: UIImageView! 

var myImageArray = [UIImage]() 
var myImageArray2 = [UIImage]() 

var back: UIImageView! 
var front: UIImageView! 

var showingFront = true 

override func viewDidLoad() { 
    super.viewDidLoad() 
    SignedIn() 

    self.view.addGestureRecognizer(revealViewController().panGestureRecognizer()) 

    let singleTap = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapped)) 
    singleTap.numberOfTapsRequired = 1 
    mainScrollView.addGestureRecognizer(singleTap) 
    mainScrollView.isUserInteractionEnabled = true 

    mainScrollView.frame = view.frame 

    myImageArray = [image1, image2 ,image3, image4, image5] 
    myImageArray2 = [imageText1, imageText2 ,imageText3, imageText4, imageText5] 

    for i in 0..<myImageArray.count { 

     front = UIImageView() 
     front.image = myImageArray[i] 
     front.contentMode = .scaleToFill 
     let yPosition = self.view.frame.height * CGFloat(i) + self.view.frame.height/2 - (self.view.frame.height/1.1)/2 
     let xPosition = self.view.frame.width/2 - (self.view.frame.width/1.1)/2 
     front.frame = CGRect(x: xPosition, y: yPosition, width: self.view.frame.width/1.1, height: self.view.frame.height/1.1) 
     front.layer.borderWidth = 5 

     back = UIImageView() 
     back.image = myImageArray2[i] 
     back.contentMode = .scaleToFill 
     back.frame = CGRect(x: xPosition, y: yPosition, width: self.view.frame.width/1.1, height: self.view.frame.height/1.1) 
     back.layer.borderWidth = 5 

     mainScrollView.contentSize.height = mainScrollView.frame.height * CGFloat(i + 1) 

     imageOne.addSubview(front) 
     imageTwo.addSubview(back) 
    } 
} 

func tapped() { 
    print("Hello") 
    if (showingFront) { 
     UIView.transition(from: imageOne, to: imageTwo, duration: 1, options: UIViewAnimationOptions.transitionFlipFromRight, completion: nil) 
     showingFront = false 
    } else { 
     UIView.transition(from: imageTwo, to: imageOne, duration: 1, options: UIViewAnimationOptions.transitionFlipFromLeft, completion: nil) 
     showingFront = true 
    } 
} 

}

回答

1

您需要添加第二個動畫選項可以保留「從」視圖中的發揮。下面是我如何做到這一點:

// declare the animation options 
let transitionOptions: UIViewAnimationOptions = [.transitionFlipFromRight, .showHideTransitionViews] 

func tapped() { 
    print("Hello") 
    if (showingFront) { 
     UIView.transition(from: imageOne, to: imageTwo, duration: 1, options: transitionOptions, completion: nil) 
     showingFront = false 
    } else { 
     UIView.transition(from: imageTwo, to: imageOne, duration: 1, options: transitionOptions, completion: nil) 
     showingFront = true 
    } 
} 

我記得,我花了一個堅實的小時添加第二個選項,斯威夫特2碼,你可以採取不同的方式。

+0

太棒了,非常感謝你:)一直卡在這一段時間。你是一個拯救生命的人! –