UIViewAnimationTransition翻转UIImageView

时间:2017-07-19 21:31:57

标签: ios swift uiview flip uiviewanimationtransition

Video

我在底部的tapped函数中的UIView.transitions第一次工作,但是我的else语句第二次翻回到正面,每次都崩溃。获取错误消息“致命错误:在解包可选值时意外发现nil”。想要成功地每次前后翻转并回到前面:)在此先感谢您的好朋友。 ;)另外,我添加了我的ImageOne& ImageTot在我的故事板中查看!

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 个答案:

答案 0 :(得分:1)

您需要添加第二个动画选项以保持&#34;免于&#34;在游戏中查看。以下是我的表现方式:

// 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
    }
}

我记得我花了一个小时来添加第二个选项,因为Swift 2代码可以用不同的方式完成。