步骤: 1.单击button1,从第一个ViewController ViewController1 导航到第二个ViewController TamilVowelsViewController 2.用户点击主页按钮,导航回 ViewController1 3. 问题: ViewController1中的所有按钮都没有响应。
TamilVowelsViewController - Screenshot
ViewController1和TamilVowelsViewController - 代码
//
// MenuViewController.swift
// Tamizh Meesai
//
// Created by Swaroop on 11/2/17.
// Copyright © 2017 Swaroop. All rights reserved.
//
import UIKit
class ViewController1: UIViewController {
let screenSize = UIScreen.main.bounds
let buttonSize: CGFloat = 150
let buttonRadius: CGFloat = 75
let buttonSpacing: CGFloat = 30
let mathImages = ["plus-1-32.png", "minus-1-32.png", "multiple-1-32.png", "minus-1-32.png"]
let buttonLabel = ["அ","க்","க","ஷ்"]
var frontCardView: CardView! = nil
var backCardView: CardView! = nil
var showingBack = true
let beautifulBluesTheme = [ UIColor(red: 1/255, green: 31/255, blue: 75/255, alpha: 1),
UIColor(red: 3/255, green: 57/255, blue: 108/255, alpha: 1),
UIColor(red: 0/255, green: 91/255, blue: 150/255, alpha: 1),
UIColor(red: 100/255, green: 151/255, blue: 177/255, alpha: 1),
UIColor(red: 179/255, green: 205/255, blue: 224/255, alpha: 1)
]
let cappuccinoTheme = [ UIColor(red: 75/255, green: 56/255, blue: 50/255, alpha: 1),
UIColor(red: 133/255, green: 68/255, blue: 66/255, alpha: 1),
UIColor(red: 255/255, green: 244/255, blue: 230/255, alpha: 1),
UIColor(red: 60/255, green: 47/255, blue: 47/255, alpha: 1),
UIColor(red: 190/255, green: 155/255, blue: 123/255, alpha: 1)
]
var selectedTheme: Int = 1
var button1 = UIButton()
var button2 = UIButton()
var button3 = UIButton()
var button4 = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
print("viewDidLoad")
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.black
selectedTheme = Int(arc4random_uniform(2))
backCardView = CardView(frame: CGRect())
backCardView.translatesAutoresizingMaskIntoConstraints = false
if(selectedTheme == 0) {
backCardView.backgroundColor = beautifulBluesTheme[Int(arc4random_uniform(5))]
}else {
backCardView.backgroundColor = cappuccinoTheme[Int(arc4random_uniform(5))]
}
backCardView.layer.cornerRadius = 30
frontCardView = CardView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width , height: UIScreen.main.bounds.height))
//frontCardView.isUserInteractionEnabled = true
frontCardView.translatesAutoresizingMaskIntoConstraints = false
if(selectedTheme == 0) {
frontCardView.backgroundColor = beautifulBluesTheme[Int(arc4random_uniform(5))]
}else {
frontCardView.backgroundColor = cappuccinoTheme[Int(arc4random_uniform(5))]
}
frontCardView.layer.cornerRadius = 30
let label1 = UILabel(frame: CGRect())
label1.translatesAutoresizingMaskIntoConstraints = false
label1.text = "தமிழ்"
label1.textAlignment = .center
label1.font = UIFont.boldSystemFont(ofSize: 72)
label1.textColor = UIColor.white
button1 = getButton(buttonId: 0)
button2 = getButton(buttonId: 1)
button3 = getButton(buttonId: 2)
button4 = getButton(buttonId: 3)
let hStack1 = UIStackView(arrangedSubviews: [button1,button2])
hStack1.translatesAutoresizingMaskIntoConstraints = false
//hStack1.isUserInteractionEnabled = true
hStack1.frame = CGRect()
hStack1.distribution = .fillEqually
hStack1.alignment = .fill
hStack1.axis = .horizontal
hStack1.spacing = buttonSpacing
let hStack2 = UIStackView(arrangedSubviews: [button3,button4])
hStack2.translatesAutoresizingMaskIntoConstraints = false
//hStack2.isUserInteractionEnabled = true
hStack2.frame = CGRect()
hStack2.distribution = .fillEqually
hStack2.alignment = .fill
hStack2.axis = .horizontal
hStack2.spacing = buttonSpacing
let vStack = UIStackView(arrangedSubviews: [hStack1,hStack2])
vStack.translatesAutoresizingMaskIntoConstraints = false
//vStack.isUserInteractionEnabled = true
vStack.frame = CGRect()
vStack.distribution = .fillEqually
vStack.alignment = .fill
vStack.axis = .vertical
vStack.spacing = buttonSpacing
let starStack = UIStackView(arrangedSubviews: [getStarImage(), getStarImage(), getStarImage(), getStarImage(), getStarImage()])
starStack.translatesAutoresizingMaskIntoConstraints = false
starStack.frame = CGRect(x: 0, y: 0, width: getStarImage().frame.width * 5 , height: getStarImage().frame.height)
starStack.distribution = .equalSpacing
starStack.axis = .horizontal
starStack.alignment = .fill
starStack.spacing = 0
let settingsButtonImage = UIImage(named: "gear-1-32.png")!.withRenderingMode(.alwaysTemplate)
let settingsButton = UIButton()
//settingsButton.isUserInteractionEnabled = true
//settingsButton.translatesAutoresizingMaskIntoConstraints = false
settingsButton.frame = CGRect(x: view.frame.width - (settingsButtonImage.size.width) - 20, y: view.frame.height - (settingsButtonImage.size.width) - 20, width: (settingsButtonImage.size.width) * 1.25, height: (settingsButtonImage.size.height) * 1.25)
settingsButton.setImage(settingsButtonImage, for: .normal)
settingsButton.contentMode = .scaleToFill
settingsButton.addTarget(self, action: #selector(cardButtonAction), for: .touchUpInside)
settingsButton.setImage(settingsButtonImage, for: .normal)
settingsButton.tintColor = UIColor(white: 1.0, alpha: 0.6)
settingsButton.tag = 4
let iapButtonImage = UIImage(named: "lock-1-32.png")!.withRenderingMode(.alwaysTemplate)
let iapButton = UIButton()
//iapButton.translatesAutoresizingMaskIntoConstraints = false
iapButton.frame = CGRect(x: 10, y: view.frame.height - (iapButtonImage.size.width) - 20 , width: (iapButtonImage.size.width) * 1.25, height: (iapButtonImage.size.height) * 1.25)
iapButton.setImage(iapButtonImage, for: .normal)
iapButton.contentMode = .scaleAspectFill
iapButton.addTarget(self, action: #selector(cardButtonAction), for: .touchUpInside)
iapButton.tintColor = UIColor(white: 1.0, alpha: 0.6)
let nextButtonImage1 = UIImage(named: "rightarrow-1-32.png")!.withRenderingMode(.alwaysTemplate)
let nextButton1 = UIButton()
//nextButton.translatesAutoresizingMaskIntoConstraints = false
nextButton1.frame = CGRect(x: view.frame.width - (nextButtonImage1.size.width) - 20, y: 10, width: (nextButtonImage1.size.width) * 1.25, height: (nextButtonImage1.size.height) * 1.25)
nextButton1.setImage(nextButtonImage1, for: .normal)
nextButton1.contentMode = .scaleAspectFill
nextButton1.addTarget(self, action: #selector(cardButtonAction), for: .touchUpInside)
nextButton1.tintColor = UIColor(white: 1.0, alpha: 0.6)
let scoreButtonImage = UIImage(named: "star-1-32.png")!.withRenderingMode(.alwaysTemplate)
let scoreButton = UIButton()
//scoreButton.translatesAutoresizingMaskIntoConstraints = false
scoreButton.frame = CGRect(x: 10, y: 10, width: (scoreButtonImage.size.width) * 1.25, height: (scoreButtonImage.size.height) * 1.25)
scoreButton.setImage(scoreButtonImage, for: .normal)
scoreButton.contentMode = .scaleAspectFill
scoreButton.addTarget(self, action: #selector(cardButtonAction), for: .touchUpInside)
scoreButton.tintColor = UIColor(white: 1.0, alpha: 0.6)
frontCardView.addSubview(label1)
frontCardView.addConstraint(NSLayoutConstraint(item: label1, attribute: .centerX, relatedBy: .equal, toItem: frontCardView, attribute: .centerX , multiplier: 1, constant: 0))
frontCardView.addConstraint(NSLayoutConstraint(item: label1, attribute: .top, relatedBy: .equal, toItem: frontCardView, attribute: .top , multiplier: 1, constant: 50))
frontCardView.addSubview(vStack)
frontCardView.addConstraint(NSLayoutConstraint(item: vStack, attribute: .centerX, relatedBy: .equal, toItem: frontCardView, attribute: .centerX , multiplier: 1, constant: 0))
frontCardView.addConstraint(NSLayoutConstraint(item: vStack, attribute: .top, relatedBy: .equal, toItem: frontCardView, attribute: .top , multiplier: 1, constant: view.frame.height * 0.25))
frontCardView.addConstraint(NSLayoutConstraint(item: vStack, attribute: .width, relatedBy: .equal, toItem: frontCardView, attribute: .width, multiplier: 0, constant: buttonSize * 2 + buttonSpacing))
frontCardView.addConstraint(NSLayoutConstraint(item: vStack, attribute: .height, relatedBy: .equal, toItem: frontCardView, attribute: .height, multiplier: 0, constant: buttonSize * 2 + buttonSpacing))
frontCardView.addSubview(starStack)
frontCardView.addConstraint(NSLayoutConstraint(item: starStack, attribute: .centerX, relatedBy: .equal, toItem: frontCardView, attribute: .centerX , multiplier: 1, constant: 0))
frontCardView.addConstraint(NSLayoutConstraint(item: starStack, attribute: .bottom, relatedBy: .equal, toItem: frontCardView, attribute: .bottom , multiplier: 1, constant: -80))
frontCardView.addSubview(settingsButton)
frontCardView.addSubview(iapButton)
frontCardView.addSubview(nextButton1)
frontCardView.addSubview(scoreButton)
view.addSubview(backCardView)
view.addConstraint(NSLayoutConstraint(item: backCardView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX , multiplier: 1, constant: view.frame.midX))
view.addConstraint(NSLayoutConstraint(item: backCardView, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY , multiplier: 1, constant: view.frame.midY))
view.addConstraint(NSLayoutConstraint(item: backCardView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0, constant: view.frame.width))
view.addConstraint(NSLayoutConstraint(item: backCardView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0, constant: view.frame.height))
view.addSubview(frontCardView)
view.addConstraint(NSLayoutConstraint(item: frontCardView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX , multiplier: 1, constant: UIScreen.main.bounds.midX))
view.addConstraint(NSLayoutConstraint(item: frontCardView, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY , multiplier: 1, constant: UIScreen.main.bounds.midY))
view.addConstraint(NSLayoutConstraint(item: frontCardView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0, constant: UIScreen.main.bounds.width))
view.addConstraint(NSLayoutConstraint(item: frontCardView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0, constant: UIScreen.main.bounds.height))
//frontCardView.updateConstraints()
//view.updateConstraints()
let singleTap = UITapGestureRecognizer(target: self, action: #selector(tapDetected) )
singleTap.numberOfTapsRequired = 1
singleTap.numberOfTouchesRequired = 1
view.isUserInteractionEnabled = true
view.addGestureRecognizer(singleTap)
//view.didMoveToWindow()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("viewWillAppear")
view.layoutSubviews()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("viewDidLayoutSubviews")
view.clipsToBounds = false
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
print("viewWillLayoutSubviews")
view.updateConstraintsIfNeeded()
view.needsUpdateConstraints()
view.updateConstraints()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touches: \(touches.count)")
for touch in touches {
let location = touch.location(in: self.view)
print("location: \(location)")
let point = self.view.convert(location, from:nil)
print("point: \(point)")
//let hitLayer = self.view.layer.name
//print("hitLayer: \(hitLayer)")
//if let hitLayer = self.view.layer.hitTest(point) {
/*if hitLayer != nil && hitLayer?.name != nil {
print(hitLayer?.name)
}
//}*/
}
}
func logViewHierarchy(view: UIView, num: Int) {
var index = num
for i in 0..<index {
print("\t")
}
index = index + 1
print("\(NSStringFromClass(type(of: view))) userInteractionEnabled: \(view.isUserInteractionEnabled)")
for subview in view.subviews {
self.logViewHierarchy(view: subview as! UIView, num: index)
}
}
override var prefersStatusBarHidden: Bool {
return true
}
@IBAction func tapDetected(_ sender: UITapGestureRecognizer) {
let fromView = showingBack ? frontCardView : backCardView
let toView = showingBack ? backCardView : frontCardView
let flipDirection: UIViewAnimationOptions = showingBack ? .transitionFlipFromRight : .transitionFlipFromLeft
let options: UIViewAnimationOptions = [flipDirection, .showHideTransitionViews]
UIView.transition(from: fromView!, to: toView!, duration: 0.5, options: options, completion: nil)
showingBack = !showingBack
if(selectedTheme == 0) {
backCardView.backgroundColor = beautifulBluesTheme[Int(arc4random_uniform(5))]
frontCardView.backgroundColor = beautifulBluesTheme[Int(arc4random_uniform(5))]
}else {
backCardView.backgroundColor = cappuccinoTheme[Int(arc4random_uniform(5))]
frontCardView.backgroundColor = cappuccinoTheme[Int(arc4random_uniform(5))]
}
}
func getButton(buttonId: Int) -> UIButton {
let button = UIButton()
//button.translatesAutoresizingMaskIntoConstraints = false
button.frame = CGRect(x: 0, y: 0, width: buttonSize, height: buttonSize)
button.layer.cornerRadius = buttonSize * 0.5
button.setTitle(buttonLabel[buttonId], for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 84)
//button.titleLabel?.textAlignment = .center
button.titleLabel?.alpha = 0.8
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
button.tintColor = UIColor.clear
button.tag = buttonId
//button.isUserInteractionEnabled = true;
//button.isEnabled = true
let blur = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.regular))
blur.frame = button.bounds
blur.isUserInteractionEnabled = false
blur.layer.cornerRadius = 0.5 * button.bounds.size.width
blur.clipsToBounds = true
button.insertSubview(blur, belowSubview: button.titleLabel!)
return button
}
func getStarImage() -> UIImageView{
let starImage = UIImage(named: "star-1-32.png")
let starImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: (starImage?.size.width)!, height: (starImage?.size.height)!))
starImageView.image = starImage
return starImageView
}
@IBAction func buttonAction(button: UIButton!) {
print("action")
button.transform = CGAffineTransform(scaleX: 0.6, y: 0.6)
UIView.animate(withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 0.5,
initialSpringVelocity: 4.0,
options: .allowUserInteraction,
animations: { [weak self] in
button.transform = .identity
},
completion: {(finished:Bool) in
if(button.tag == 0) {
let tamilVowelsViewController = TamilVowelsViewController()
self.navigationController?.pushViewController(tamilVowelsViewController, animated: false)
}
})
}
@IBAction func cardButtonAction(button: UIButton!) {
button.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
UIView.animate(withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 0.5,
initialSpringVelocity: 4.0,
options: .allowUserInteraction,
animations: { [weak self] in
button.transform = .identity
},
completion: nil)
}
}
**TamilVowelsViewController**
//
// TamilVowelsViewController.swift
// Tamizh Meesai
//
// Created by Swaroop on 11/5/17.
// Copyright © 2017 Swaroop. All rights reserved.
//
import UIKit
class TamilVowelsViewController: UIViewController {
var vowelsCardView: CardView! = nil
var frontCardView: CardView! = nil
var backCardView: CardView! = nil
let buttonSize: CGFloat = 100
let hButtonSpacing: CGFloat = 30
let vButtonSpacing: CGFloat = 10
let tamilLetters: TamilLetters = TamilLetters()
override func viewDidLoad() {
vowelsCardView = CardView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
//vowelsCardView.translatesAutoresizingMaskIntoConstraints = false
vowelsCardView.backgroundColor = UIColor.blue
vowelsCardView.layer.cornerRadius = 30
let label1 = UILabel(frame: CGRect())
label1.translatesAutoresizingMaskIntoConstraints = false
label1.text = "உயிர்"
label1.textAlignment = .center
label1.font = UIFont.boldSystemFont(ofSize: 56)
label1.alpha = 0.8
label1.textColor = UIColor.white
var lettersStack: UIStackView = UIStackView()
let vStack = UIStackView()
vStack.translatesAutoresizingMaskIntoConstraints = false
vStack.frame = CGRect()
vStack.distribution = .fillEqually
vStack.alignment = .fill
vStack.axis = .vertical
vStack.spacing = hButtonSpacing
var vowelId: Int = 0
var vowelCount = tamilLetters.vowels.count
for vowel in tamilLetters.vowels {
let vowelButton = getVowelButton(vowelId: vowelId)
if(vowelId % 3 == 0) {
//print("1")
lettersStack = UIStackView()
lettersStack.frame = CGRect()
lettersStack.distribution = .fillEqually
lettersStack.alignment = .fill
lettersStack.axis = .horizontal
lettersStack.spacing = hButtonSpacing
vStack.addArrangedSubview(lettersStack)
lettersStack.addArrangedSubview(vowelButton)
}else {
//print("2")
lettersStack.addArrangedSubview(vowelButton)
}
vowelId = vowelId + 1
}
let homeButtonImage = UIImage(named: "home-3-48.png")!.withRenderingMode(.alwaysTemplate)
let homeButton = UIButton()
//scoreButton.translatesAutoresizingMaskIntoConstraints = false
homeButton.frame = CGRect(x: 10, y: 10, width: (homeButtonImage.size.width) * 1.25, height: (homeButtonImage.size.height) * 1.25)
homeButton.setImage(homeButtonImage, for: .normal)
homeButton.contentMode = .scaleAspectFill
homeButton.addTarget(self, action: #selector(cardButtonAction), for: .touchUpInside)
homeButton.tintColor = UIColor(white: 1.0, alpha: 0.6)
homeButton.tag = 0
vowelsCardView.addSubview(homeButton)
vowelsCardView.addSubview(label1)
vowelsCardView.addConstraint(NSLayoutConstraint(item: label1, attribute: .centerX, relatedBy: .equal, toItem: vowelsCardView, attribute: .centerX , multiplier: 1, constant: 0))
vowelsCardView.addConstraint(NSLayoutConstraint(item: label1, attribute: .top, relatedBy: .equal, toItem: vowelsCardView, attribute: .top , multiplier: 1, constant: 5))
vowelsCardView.addSubview(vStack)
vowelsCardView.addConstraint(NSLayoutConstraint(item: vStack, attribute: .centerX, relatedBy: .equal, toItem: vowelsCardView, attribute: .centerX , multiplier: 1, constant: 0))
vowelsCardView.addConstraint(NSLayoutConstraint(item: vStack, attribute: .top, relatedBy: .equal, toItem: vowelsCardView, attribute: .top , multiplier: 1, constant: view.frame.height * 0.11))
vowelsCardView.addConstraint(NSLayoutConstraint(item: vStack, attribute: .width, relatedBy: .equal, toItem: vowelsCardView, attribute: .width, multiplier: 0, constant: buttonSize * 3 + hButtonSpacing * 2))
vowelsCardView.addConstraint(NSLayoutConstraint(item: vStack, attribute: .height, relatedBy: .equal, toItem: vowelsCardView, attribute: .height, multiplier: 0, constant: buttonSize * 4 + vButtonSpacing * 3))
frontCardView = CardView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
//frontCardView.translatesAutoresizingMaskIntoConstraints = false
frontCardView.backgroundColor = UIColor.brown
frontCardView.layer.cornerRadius = 25
view.addSubview(vowelsCardView)
/*view.addConstraint(NSLayoutConstraint(item: vowelsCardView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX , multiplier: 1, constant: view.frame.midX))
view.addConstraint(NSLayoutConstraint(item: vowelsCardView, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY , multiplier: 1, constant: view.frame.midY))
view.addConstraint(NSLayoutConstraint(item: vowelsCardView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0, constant: view.frame.width))
view.addConstraint(NSLayoutConstraint(item: vowelsCardView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0, constant: view.frame.height))
view.updateConstraints()
view.layoutIfNeeded()*/
}
override func didReceiveMemoryWarning() {
}
override var prefersStatusBarHidden: Bool {
return true
}
func getVowelButton(vowelId: Int) -> UIButton {
let button = UIButton()
//button.translatesAutoresizingMaskIntoConstraints = false
button.frame = CGRect(x: 0, y: 0, width: buttonSize, height: buttonSize)
button.layer.cornerRadius = buttonSize * 0.5
//button1.setImage(UIImage(named: "mathImages[0]", for: .normal)
button.setTitle(tamilLetters.vowels[vowelId], for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 56)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
button.tintColor = UIColor.clear
button.tag = vowelId
let blur = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.light))
blur.frame = button.bounds
blur.isUserInteractionEnabled = false
blur.layer.cornerRadius = 0.5 * button.bounds.size.width
blur.clipsToBounds = true
button.insertSubview(blur, belowSubview: button.titleLabel!)
return button
}
@IBAction func buttonAction(sender: UIButton!) {
}
@IBAction func cardButtonAction(button: UIButton!) {
/*button.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
UIView.animate(withDuration: 0.5,
delay: 0,
usingSpringWithDamping: 0.5,
initialSpringVelocity: 4.0,
options: [.allowUserInteraction],
animations: { [weak self] in
button.transform = .identity
},
completion: nil)*/
if(button.tag == 0) {
self.navigationController?.popViewController(animated: true)
self.navigationController?.interactivePopGestureRecognizer?.delegate = self as? UIGestureRecognizerDelegate
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true;
//self.view.clipsToBounds = true;
}
}
}
步骤: 1.单击button1,从第一个ViewController ViewController1 导航到第二个ViewController TamilVowelsViewController 2.用户点击主页按钮,导航回 ViewController1 3. 问题: ViewController1中的所有按钮都没有响应。