UIViewAnimationTransition 翻转 UIImageView
UIViewAnimationTransition flip UIImageView
我的 UIView.transitions 在底部的点击函数中第一次工作,但第二次我的 else 语句返回到前端时每次都会崩溃。收到错误消息 "fatal error: unexpectedly found nil while unwrapping an Optional value"。想要每次都成功地从前到后和从后到前翻转:)提前谢谢你的朋友。 ;) 另外,我在故事板中添加了 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
}
}
}
您需要添加第二个动画选项以保持 "from" 视图播放。以下是我的做法:
// 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
}
}
我记得我花了整整一个小时来添加第二个选项,因为 Swift2 代码你可以用不同的方式来做。
我的 UIView.transitions 在底部的点击函数中第一次工作,但第二次我的 else 语句返回到前端时每次都会崩溃。收到错误消息 "fatal error: unexpectedly found nil while unwrapping an Optional value"。想要每次都成功地从前到后和从后到前翻转:)提前谢谢你的朋友。 ;) 另外,我在故事板中添加了 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
}
}
}
您需要添加第二个动画选项以保持 "from" 视图播放。以下是我的做法:
// 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
}
}
我记得我花了整整一个小时来添加第二个选项,因为 Swift2 代码你可以用不同的方式来做。