I am currently making a biography program in Swift. The code below is fully func
ID: 3861687 • Letter: I
Question
I am currently making a biography program in Swift. The code below is fully functional. When one of the buttons on the main screen is clicked, it will use a transition to show the corresponding page, and the back button on each of these pages also uses a transition to bring it back to the home screen. The problem with this however, is that it only occurs once. I have tried using .repeat and .autoreverse, but this causes it to get caught in a never ending loop that is clearly visible. What do I have to change in this code to make the animation appear every time I click the button?
Please use the attached code directly. Thanks.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//: Playground - noun: a place where people can play
import UIKit
import XCPlayground
import PlaygroundSupport
import MapKit
PlaygroundPage.current.needsIndefiniteExecution = true
//CONTAINER
let new = UIViewController()
let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 375.0, height: 667.0))
//------------------------------------------
//MAKES THE VIEW----------------------------
PlaygroundPage.current.liveView = containerView
let view = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568)) // iPhone 5 proportions
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(blurEffectView)
view.backgroundColor = UIColor(patternImage: UIImage(named: "water.jpg")!)
//------------------------------------------
// AALAP PATEL LABEL------------------------
let aalapLabel = UILabel(frame: CGRect(x:150, y: -40, width: 150, height: 44))
aalapLabel.text = "Aalap Patel"
aalapLabel.textColor = UIColor.black
view.addSubview(aalapLabel)
aalapLabel.font = UIFont(name:"HelveticaNeue-Thin",size:30)
aalapLabel.textAlignment = .center
//------------------------------------------
//LINE DIVIDER -----------------------------
let divider = UILabel(frame: CGRect(x:45, y: 125, width: 300, height: 44))
divider.text = "________________________________"
divider.textColor = UIColor.black
view.addSubview(divider)
//------------------------------------------
//WHO AM I LABEL----------------------------
let whoLabel = UILabel(frame: CGRect(x:320, y: 200, width: 300, height: 44))
whoLabel.font = UIFont(name:"HelveticaNeue-Thin", size: 30)
whoLabel.text = "Who Am I?"
whoLabel.textColor = UIColor.black
view.addSubview(whoLabel)
//------------------------------------------
//PROJECTS LABEL----------------------------
let projectsLabel = UILabel(frame: CGRect(x:-150, y: 315, width: 300, height: 50))
projectsLabel.font = UIFont(name:"HelveticaNeue-Thin", size: 30)
projectsLabel.text = "My Projects"
projectsLabel.textColor = UIColor.black
view.addSubview(projectsLabel)
//------------------------------------------
//QUIZ LABEL -------------------------------
let quizLabel = UILabel(frame: CGRect(x:340, y: 440, width: 300, height: 44))
quizLabel.font = UIFont(name:"HelveticaNeue-Thin", size: 30)
quizLabel.text = "Quiz"
quizLabel.textColor = UIColor.black
view.addSubview(quizLabel)
//------------------------------------------
//WHO AM I PAGE-----------------------------
let whoPage = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568)) // iPhone 5 proportions
let blurEffect1 = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurEffectView1 = UIVisualEffectView(effect: blurEffect)
blurEffectView1.frame = whoPage.bounds
blurEffectView1.autoresizingMask = [.flexibleWidth, .flexibleHeight]
whoPage.addSubview(blurEffectView1)
whoPage.backgroundColor = UIColor(patternImage: UIImage(named: "water.jpg")!)
//------------------------------------------
//PROJECTS PAGE-----------------------------
let projectsPage = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 568)) // iPhone 5 proportions
projectsPage.backgroundColor = UIColor.white
//------------------------------------------
//QUIZ PAGE---------------------------------
let quizPage = UIView(frame: CGRect(x:0, y:0, width: 320, height: 568))
quizPage.backgroundColor = UIColor.white
//------------------------------------------
//BUTTON FUNCTIONS--------------------------
class Responder: NSObject {
func takeToWho() {
UIView.animate(withDuration: 0.5,delay:0, animations: { () -> Void in
let movePage = CGAffineTransform(translationX: 0, y: -600)
whoPage.transform = movePage
})
PlaygroundPage.current.liveView = whoPage
for _ in 0..<15 {
print("")
}
}
func takeToHome() {
UIView.animate(withDuration: 0.5, animations: { () -> Void in
let goHome = CGAffineTransform(translationX: 0, y: 600)
view.transform = goHome
})
PlaygroundPage.current.liveView = view
}
func takeToProjects(){
UIView.animate(withDuration: 0.3, animations: { () -> Void in
let movePage = CGAffineTransform(translationX: 0, y: -600)
projectsPage.transform = movePage
})
PlaygroundPage.current.liveView = projectsPage
}
func takeToQuiz(){
UIView.animate(withDuration: 0.3, animations: { () -> Void in
let movePage = CGAffineTransform(translationX: 0, y: -600)
quizPage.transform = movePage
})
PlaygroundPage.current.liveView = quizPage
for _ in 0..<15 {
print("")
}
}
}
//------------------------------------------
let responder = Responder()
// Who AM I BUTTON--------------------------
let button = UIButton(type: .system)
button.setTitle("", for: .normal)
button.addTarget(responder, action: #selector(Responder.takeToWho), for: .touchUpInside)
button.titleLabel?.font = UIFont(name:"Helvetica", size: 25)
button.sizeToFit()
button.frame = CGRect(x: 25, y: 170, width: 100, height: 100)
button.layer.cornerRadius = button.frame.size.width/2
button.clipsToBounds = true
view.addSubview(button)
//------------------------------------------
//BACK BUTTON FOR WHO AM I------------------
let button1 = UIButton(type: .system)
button1.setTitle("Back", for: .normal)
button1.addTarget(responder, action: #selector(Responder.takeToHome), for: .touchUpInside)
button1.titleLabel?.font = UIFont(name:"Helvetica", size: 20)
button1.sizeToFit()
button1.center = CGPoint(x: 40, y: 30)
whoPage.addSubview(button1)
//------------------------------------------
//BUTTON FOR PROJECTS PAGE------------------
let button3 = UIButton(type: .system)
button3.setTitle("", for: .normal)
button3.addTarget(responder, action: #selector(Responder.takeToProjects), for: .touchUpInside)
button3.titleLabel?.font = UIFont(name:"Helvetica", size: 25)
button3.sizeToFit()
button3.frame = CGRect(x: 195, y: 290, width: 100, height: 100)
button3.layer.cornerRadius = button3.frame.size.width/2
button3.clipsToBounds = true
view.addSubview(button3)
//------------------------------------------
//BUTTON FOR QUIZ PAGE----------------------
let button4 = UIButton(type: .system)
button4.setTitle("", for: .normal)
button4.addTarget(responder, action: #selector(Responder.takeToQuiz), for: .touchUpInside)
button4.titleLabel?.font = UIFont(name:"Helvetica", size: 25)
button4.sizeToFit()
button4.frame = CGRect(x:30, y: 415, width: 90, height: 90)
button4.backgroundColor = UIColor.clear
button4.layer.cornerRadius = button4.frame.size.width/2
button4.clipsToBounds = true
view.addSubview(button4)
//------------------------------------------
//BACK BUTTON FOR PROJECTS------------------
let backProjects = UIButton(type: .system)
backProjects.setTitle("Back", for: .normal)
backProjects.addTarget(responder, action: #selector(Responder.takeToHome), for: .touchUpInside)
backProjects.titleLabel?.font = UIFont(name:"Helvetica", size: 20)
backProjects.sizeToFit()
backProjects.center = CGPoint(x: 40, y: 30)
projectsPage.addSubview(backProjects)
//------------------------------------------
//BACK BUTTON FOR QUIZ
let backQuiz = UIButton(type: .system)
backQuiz.setTitle("Back", for: .normal)
backQuiz.addTarget(responder, action: #selector(Responder.takeToHome), for: .touchUpInside)
backQuiz.titleLabel?.font = UIFont(name:"Helvetica", size: 20)
backQuiz.sizeToFit()
backQuiz.center = CGPoint(x: 40, y: 30)
quizPage.addSubview(backQuiz)
//------------------------------------------
//PERSONAL PIC------------------------------
let aalap = UIImageView(frame: CGRect(x: 70, y: 70, width: 25, height: 25))
aalap.image = UIImage(named:"profile.png")
view.addSubview(aalap)
aalap.layer.cornerRadius = aalap.frame.size.width/4
aalap.clipsToBounds = true
//------------------------------------------
//SILHOUETTE PIC
let profile = UIImageView(frame: CGRect(x: 30, y: 180, width: 90, height: 90))
profile.image = UIImage(named:"avatar.jpg")
view.addSubview(profile)
profile.layer.cornerRadius = profile.frame.size.width/2
profile.clipsToBounds = true
//------------------------------------------
//CODING PIC--------------------------------
let code = UIImageView(frame: CGRect(x: 200, y: 300, width: 90, height: 90))
code.image = UIImage(named:"coding.png")
view.addSubview(code)
code.layer.cornerRadius = code.frame.size.width/2
code.clipsToBounds = true
//------------------------------------------
//QUIZ PIC----------------------------------
let quizPic = UIImageView(frame: CGRect(x: 30, y: 415, width: 90, height: 90))
quizPic.image = UIImage(named:"quiz.png")
quizPic.layer.cornerRadius = quizPic.frame.size.width/2
quizPic.clipsToBounds = true
view.addSubview(quizPic)
//------------------------------------------
//ABOUT ME HEADER FOR WHO AM I--------------
let about = UILabel(frame: CGRect(x:0, y: 35, width: 320, height: 44))
about.font = UIFont(name:"HelveticaNeue-Thin", size: 30)
about.textAlignment = .center
about.text = "About Me"
about.textColor = UIColor.black
whoPage.addSubview(about)
//------------------------------------------
//ANIMATIONS 2 SECONDS----------------------
UIView.animate(withDuration: 2.0, animations: { () -> Void in
let scaleTransform = CGAffineTransform(scaleX: 4.5, y: 4.5)
aalap.transform = scaleTransform
let rotationTransform = CGAffineTransform(rotationAngle: 3.14)
let offScreenRight = CGAffineTransform(translationX: containerView.frame.width, y: 0)
let offScreenLeft = CGAffineTransform(translationX: -containerView.frame.width, y: 0)
profile.transform = rotationTransform
code.transform = rotationTransform
quizPic.transform = rotationTransform
let moveDown = CGAffineTransform(translationX: 0, y: 100)
let moveLeft = CGAffineTransform (translationX: -170, y: 0)
let moveRight = CGAffineTransform(translationX: 180, y: 0)
aalapLabel.transform = moveDown
whoLabel.transform = moveLeft
quizLabel.transform = moveLeft
projectsLabel.transform = moveRight
})
//------------------------------------------
let circle = UITextView(frame: CGRect(x: 50, y: 120, width: 100, height: 100))
circle.layer.cornerRadius = circle.frame.size.width/2
circle.clipsToBounds = true
var progressCircle = CAShapeLayer()
let circlePath = UIBezierPath(ovalIn: circle.bounds)
progressCircle = CAShapeLayer ()
progressCircle.path = circlePath.cgPath
progressCircle.strokeColor = UIColor.blue.cgColor
progressCircle.fillColor = UIColor.clear.cgColor
progressCircle.lineWidth = 10.0
circle.layer.addSublayer(progressCircle)
projectsPage.addSubview(circle)
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0
animation.toValue = 0.75
animation.duration = 2
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = false
circle.transform = CGAffineTransform(rotationAngle: -CGFloat.pi/2)
progressCircle.add(animation, forKey: "ani")
//ABOUT ME FOR WHO AM I --------------------
let aboutMe = UITextView(frame: CGRect(x: 10, y: 0, width: 300, height: 140))
aboutMe.backgroundColor = UIColor.clear
aboutMe.font = UIFont(name: "HelveticaNeue", size: 14)
aboutMe.text = "Hello! My name is Aalap Patel and I currently live in beautiful Nashua, New Hampshire. I am currently 16 years old and I enjoy coding, playing basketball, and volunteering. My interests typically involve science, mathematics, and economics."
aboutMe.textAlignment = .center
aboutMe.isUserInteractionEnabled = false
//------------------------------------------
//SCHOOL INFORMATION TEXT-------------------
let school = UITextView(frame: CGRect(x:18, y: 285, width: 130, height: 130))
school.backgroundColor = UIColor.clear
school.text = "I currently attend Nashua High School South as a junior, which I have been attending for three years now. "
school.font = UIFont(name: "HelveticaNeue", size: 14)
school.isUserInteractionEnabled = false
//------------------------------------------
//SCHOOL IMAGE
let nhss = UIImageView (frame: CGRect(x: 165, y: 285, width: 140, height: 100))
nhss.image = UIImage(named: "nhss.jpg")
nhss.layer.cornerRadius = nhss.frame.size.width/8
nhss.clipsToBounds = true
//------------------------------------------
//SCROLL VIEW FOR WHO AM I-----------------
let scroll = UIScrollView(frame: CGRect(x: 0, y: 80, width: 320, height: 500))
scroll.contentSize = CGSize(width: 320, height: 1000)
scroll.backgroundColor = UIColor.clear
scroll.isUserInteractionEnabled = true
scroll.addSubview(aboutMe)
whoPage.addSubview(scroll)
scroll.addSubview(school)
scroll.addSubview(nhss)
//------------------------------------------
//MAP VIEW----------------------------------
let map = MKMapView()
map.isUserInteractionEnabled = true
map.mapType = .standard
map.frame = (frame: CGRect(x: 25, y: 125, width: 270, height: 125))
scroll.addSubview(map)
func addPin() {
let annotation = MKPointAnnotation()
let centerCoordinate = CLLocationCoordinate2D(latitude: 42.7579, longitude:-71.4640)
annotation.coordinate = centerCoordinate
annotation.title = "Nashua, NH"
annotation.subtitle = "I've lived here for 3 years."
map.addAnnotation(annotation)
}
func focusMapView() {
let mapCenter = CLLocationCoordinate2DMake(42.7579,-71.4640)
let span = MKCoordinateSpanMake(1.6,1.6)
let region = MKCoordinateRegionMake(mapCenter, span)
map.region = region
}
map.layer.cornerRadius = map.frame.size.width/6
map.clipsToBounds = true
addPin()
focusMapView()
//------------------------------------------
//SEGMENTED CONTROL FOR PROJECTS-----------
let textView = UITextView()
textView.frame = (frame: CGRect(x: 60, y: 80, width: 50, height: 50))
textView.font = UIFont(name: "Helvetica", size: 30)
textView.backgroundColor = UIColor.white
projectsPage.addSubview(textView)
textView.isUserInteractionEnabled = false
/*
let pageControl = UIPageControl()
pageControl.frame = (frame: CGRect(x: 100, y: 60, width: 100, height: 30))
pageControl.numberOfPages = 3
pageControl.currentPageIndicatorTintColor = UIColor.black
pageControl.pageIndicatorTintColor = UIColor.blue
let counter = pageControl.currentPage
quizPage.addSubview(pageControl)
if (counter == 0){
textView.text = "Page 1!"
}
else if (counter == 1){
textView.text = "Page 2!"
}
else if (counter == 2){
textView.text = "Page 3!"
}
*/
//------------------------------------------
PlaygroundPage.current.liveView = view
Explanation / Answer
instead of coding for the navigation, use segue which allow the navigation in both the directions and infinite times.
once you create a segue from the button to the next view, it creates an action, and clicking on that button takes us to the connected view.
This is the easiest method I have used always.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.