Swift 4 - The background function isn't working - ios

I made this simple game with Swift 4 but it's not working.
Code :
class ViewController: UIViewController {
#IBOutlet weak var TheCharacter: UIImageView!
#IBOutlet weak var rocket1: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.rocket1.isHidden = true
if 1 % 1 == 0 {
if 0 == 0{
self.rocket1.isHidden = false
let xPosR = rocket1.frame.origin.x + 500
let yPosR = rocket1.frame.origin.y
let heightCharacterR = rocket1.frame.size.height
let widthCharacterR = rocket1.frame.size.width
UIView.animate(withDuration: 1.75, animations: {
self.rocket1.frame = CGRect(x: xPosR, y: yPosR
, width: widthCharacterR, height: heightCharacterR)
}) { (finished) in
}
self.rocket1.isHidden = true
self.rocket1.frame = CGRect(x: xPosR - 500, y: yPosR, width: widthCharacterR, height: heightCharacterR)
}
}
}
}
I know it's being written badly but the problem is one of the rockets has to move in my screen (from one side to the other) because the condition is always true since 0==0 and 1 % 1 == 0. But it's not moving.
Why?

You are animating the views in viewDidLoad(), that is, before they are visible in the window. Move that code to viewDidAppear(). Then your views are visible and can be animated.
And be sure your views are not using autolayout that constraint the frame. Animating the frame is not a good idea OTOH.

Related

Swift: Changing (translate) a UIView position through a pan gesture in its superview window

Introduction
Context:
In my main ViewController I have a scrollView with a few objects inside (which are UIViews). When one of the UIViews are tapped/selected I animate forward a UITextView in a UIView to go with the selected object. (only one UIView can appear at a time)
This UIView that appears on object selection is separated into a separate class called AdjunctiveTextView.
Issue/goal:
(the example code provided below will clear make this clear, I've also commented where the issue lies in the code)
When an object has been tapped and has an adjacent UIView with a text I want to have that adjacent UIView to follow with the scrollView.
I'm using a UIPanGestureRecognizer to attempt to do this. But I can't figure out how to make it work when the user drags in the scrollview. It only work if the user drags on the actual adjunctiveTextView.
Everything works as expected except that the adjunctiveTextView does not change its position during the panGesture.
I would like (if possible) to have the AdjunctiveTextView as a separate class. My ViewController file is getting rather big.
Question:
Why doesn't the UIPanGestureRecognizer work as expected? What is needed in order for it to translate the backView correctly?
Code
My attempt: (as shown below)
My attempt simply makes the backView itself "dragable" around through the panGesture. Nothing happens to it when I scroll the scrollView.
(I have only included relevant portions of my code)
class ViewController: UIViewController {
let adjunctiveTextView = AdjunctiveTextView()
// this is a delegate method which gets called when an object is tapped in the scrollView
func scrollViewObjectIsTapped(_ objectScrollView: ObjectScrollView, object: AvailableObject) {
** adjunctiveTextView.scrollView = scrollView // **Edited! (scrollView is the name of the scrollView in this class too)
adjunctiveTextView.showView(passInObject: AvailableObject)
}
}
class AdjunctiveTextView: NSObject {
lazy var backView: UIView = {
//backView setup
}
lazy var textView: UITextView = {
//textView setup
}
//additional init and setup
** weak var scrollView : UIScrollView! // **Edited!
func showView(passInObject: AvailableObject) {
if let window = UIApplication.shared.keyWindow {
// the issue must either be here in the PanGesture setup
let panG = UIPanGestureRecognizer(target: self, action: #selector(translateView(sender:)))
panG.cancelsTouchesInView = false
// window.addGestureRecognizer(panG)
** scrollView.addGestureRecognizer(panG) // **Edited!
window.addSubview(backView)
textView.text = passInObject.information
backView.frame = CGRect(x: passInObject.frame.minX, y: passInObject.minY, width: window.frame.width - passInObject.maxX - 6, height: textView.bounds.height + 5)
backView.alpha = 0
//it animates a change of the backViews x position and alpha.
UIView.animate(withDuration: 0.42, delay: 0, options: .curveEaseInOut, animations: {
self.backView.alpha = 1
self.backView.frame = CGRect(x: passInObject.frame.minX + passInObject.frame.width, y: passInObject.minY, width: window.frame.width - passInObject.maxX - 6, height: textView.bounds.height + 5)
}, completion: nil)
}
}
// or the issue is here in the handle function for the PanGesture.
#objc private func translateView(sender: UIPanGestureRecognizer) {
if let window = UIApplication.shared.keyWindow {
let translation = sender.translation(in: window) //Have tried setting this to scrollView also
switch sender.state {
case .began, .changed:
backView.center = CGPoint(x: backView.center.x, y: backView.center.y + translation.y)
sender.setTranslation(CGPoint.zero, in: window) //Have tried setting this to sccrollView also
break
case .ended:
break
default:
break
}
}
}
}
Thanks for reading my question.
I just add a weak reference to your scrollView and then add the pan gesture to scrollView. It works as you want. You may consider add another pan gesture to the back view if you want your original behavior.
class AdjunctiveTextView: NSObject {
lazy var backView: UIView = {
//backView setup
return UIView.init()
}()
lazy var textView: UITextView = {
//textView setup
return UITextView.init(frame: CGRect.init(x: 0, y: 0, width: 300, height: 100))
}()
weak var scrollView: UIScrollView!
//additional init and setup
func showView(passInObject: AvailableObject) {
if let window = UIApplication.shared.keyWindow {
// the issue must either be here in the PanGesture setup
let panG = UIPanGestureRecognizer(target: self, action: #selector(translateView(sender:)))
panG.cancelsTouchesInView = false
// passInObject.addGestureRecognizer(panG)
scrollView.addGestureRecognizer(panG)
window.addSubview(backView)
textView.text = passInObject.information
textView.backgroundColor = UIColor.blue
backView.addSubview(textView)
backView.frame = CGRect(x: passInObject.frame.minX, y: passInObject.frame.minY, width: window.frame.width - passInObject.frame.maxX - 6, height: textView.bounds.height + 5)
backView.alpha = 0
//it animates a change of the backViews x position and alpha.
UIView.animate(withDuration: 0.42, delay: 0, options: .curveEaseInOut, animations: {
self.backView.alpha = 1
self.backView.frame = CGRect(x: passInObject.frame.minX + passInObject.frame.width , y: passInObject.frame.minY , width: window.frame.width - passInObject.frame.maxX - 6, height: self.textView.bounds.height + 5)
self.backView.backgroundColor = UIColor.red
}, completion: nil)
}
}
// or the issue is here in the handle function for the PanGesture.
#objc private func translateView(sender: UIPanGestureRecognizer) {
if let window = UIApplication.shared.keyWindow {
let translation = sender.translation(in: window)
switch sender.state {
case .began, .changed:
backView.center = CGPoint(x: backView.center.x, y: backView.center.y + translation.y)
sender.setTranslation(CGPoint.zero, in: window)
break
case .ended:
break
default:
break
}
}
}
}
class ObjectScrollView: UIScrollView{
}
class AvailableObject: UIView{
var information: String!
}
class MySCNViewController: UIViewController {
#IBOutlet weak var oScrollView: ObjectScrollView!
// this is a delegate method which gets called when an object is tapped in the scrollView
func scrollViewObjectIsTapped(_ objectScrollView: ObjectScrollView, object: AvailableObject) {
adjunctiveTextView.showView(passInObject: object)
}
let adjunctiveTextView = AdjunctiveTextView()
let ao = AvailableObject.init(frame: CGRect.init(x: 0, y: 0, width: 200, height: 200))
override func viewDidLoad() {
super.viewDidLoad()
ao.information = "test"
adjunctiveTextView.scrollView = oScrollView
ao.backgroundColor = UIColor.yellow
}
#IBAction func tap(_ sender: Any?){
scrollViewObjectIsTapped(oScrollView, object: ao)}
}

Image Zoom Scale is Not Resetting

So I have two buttons (which have images attached to them) in my view controller. When either is clicked there is a centered popup of that image.
The problem is, that the 1st button's image does not reset its zoom scale and position after use (the second does). So when you click on the image the second time, it is still zoomed in, and misaligned.
Here is the code for the zoom feature only:
//popup window
#IBOutlet var imageView1: UIView!
#IBOutlet var imageView2: UIView!
//scroll view
#IBOutlet weak var scrollView1: UIScrollView!
#IBOutlet weak var scrollView2: UIScrollView!
//image
#IBOutlet weak var zoomImageView1: UIImageView!
#IBOutlet weak var zoomImageView2: UIImageView!
//background is dimmed when the popup window is active
#IBOutlet weak var backgroundButton: UIButton!
var button1Pressed = false
var button2Pressed = false
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView1.minimumZoomScale = 1.0
self.scrollView1.maximumZoomScale = 6.0
self.scrollView2.minimumZoomScale = 1.0
self.scrollView2.maximumZoomScale = 6.0
}
//this might be the problem code, not sure how to fix it though
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
if button1Pressed == true {
return self.zoomImageView1
} else {
return self.zoomImageView2
}
}
//resizes zoomed image when orientation changes
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
if UIDevice.current.orientation.isLandscape{
imageView1.center = self.view.center
imageView2.center = self.view.center
imageView1.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
imageView2.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
scrollView1.zoomScale = 1.0
scrollView2.zoomScale = 1.0
} else if UIDevice.current.orientation.isPortrait{
imageView1.center = self.view.center
imageView2.center = self.view.center
imageView1.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
imageView2.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
scrollView1.zoomScale = 1.0
scrollView2.zoomScale = 1.0
}
}
//activates the 1st image
#IBAction func showImageView1(_ sender: Any) {
animateIn1()
button1Pressed = true
}
//activates the 2nd image
#IBAction func showImageView2(_ sender: Any) {
animateIn2()
button2Pressed = true
}
//closes either image
#IBAction func closeImageView(_ sender: Any) {
animateOut()
button1Pressed = false
button2Pressed = false
}
func animateIn1() {
self.scrollView1.zoomScale = 1.0
self.view.addSubview(imageView1)
imageView1.center = self.view.center
imageView1.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
imageView1.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
imageView1.alpha = 0
self.backgroundButton.alpha = 0.7
UIView.animate(withDuration: 0.4) {
self.imageView1.alpha = 1
self.imageView1.transform = CGAffineTransform.identity
}
}
func animateIn2() {
self.scrollView2.zoomScale = 1.0
self.view.addSubview(imageView2)
imageView2.center = self.view.center
imageView2.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
imageView2.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3)
imageView2.alpha = 0
self.backgroundButton.alpha = 0.7
UIView.animate(withDuration: 0.4) {
self.imageView2.alpha = 1
self.imageView2.transform = CGAffineTransform.identity
}
}
func animateOut() {
if button1Pressed == true {
UIView.animate(withDuration: 0.3, animations: {
self.imageView1.transform = CGAffineTransform(scaleX: 1, y: 1)
self.imageView1.alpha = 0
self.backgroundButton.alpha = 0
}) { (success:Bool) in
self.imageView1.removeFromSuperview()
}
} else if button2Pressed == true {
UIView.animate(withDuration: 0.3, animations: {
self.imageView2.transform = CGAffineTransform(scaleX: 1, y: 1)
self.imageView2.alpha = 0
self.backgroundButton.alpha = 0
}) { (success:Bool) in
self.imageView2.removeFromSuperview()
}
}
}
It's probably something simple.
Any help would be greatly appreciated.
Instead of checking for button1Pressed == true you should rather check which scrollview is given as an argument:
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
if scrollView == scrollView1 {
return self.zoomImageView1
} else {
return self.zoomImageView2
}
}

loop reseting in scrollview (swift3)

My code is a uiscrolview with two pages and a button. The problem is that if I am on the view controller info (middle image). Then hit the button to go back to scrollview it goes back to page 1. So no matter what going from the info slide to the scrollview it always goes back to view controller 1 and the loop resets. If I am on vc 2 hit the info button go to the info slide.If I hit the button on the info slide I want to return to vc 2. My code is below. Essentially the loop always resets I want to fix that if possible.
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet var pageControl: UIPageControl!
#IBOutlet var slideScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
slideScrollView.delegate = self
let slides:[slide] = createSlides()
setupSlideScrollView(slides: slides)
pageControl.numberOfPages = slides.count
pageControl.currentPage = 0
view.bringSubview(toFront: pageControl)
}
override var prefersStatusBarHidden: Bool {
return true
}
func createSlides() -> [slide]{
let slide1:slide = Bundle.main.loadNibNamed("slide", owner: self, options: nil)?.first as! slide
slide1.label.text = "1"
let slide2:slide = Bundle.main.loadNibNamed("slide", owner: self, options: nil)?.first as! slide
slide2.label.text = "2"
return [slide1,slide2 ]
}
func setupSlideScrollView(slides:[slide]) {
slideScrollView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
slideScrollView.contentSize = CGSize(width: view.frame.width * CGFloat(slides.count), height: view.frame.height)
for i in 0..<slides.count{
slides[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
slideScrollView.addSubview(slides[i])
slideScrollView.isPagingEnabled = true
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex = round(scrollView.contentOffset.x/view.frame.width)
pageControl.currentPage = Int(pageIndex)
}}
This sounds like a ViewWillAppear or ViewWillAppear problem , check if you are setting anything in those functions.

Starting an action when View Controller Loads

How do I start an action when the view controller loads on screen?
I've managed to do the function I want with an #IBAction but I don't want a button press for the action to happen, I want it to start the action when the page loads
any thoughts?
class ViewController: UIViewController {
var progress: KDCircularProgress!
#IBOutlet weak var Label1: UILabel!
var LabelText = String()
var scorestart = 1.0
var anglepercent = 3.6
override func viewDidLoad() {
super.viewDidLoad()
Label1.text = LabelText
view.backgroundColor = UIColor(white: 0.22, alpha: 1)
progress = KDCircularProgress(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
progress.startAngle = -90
progress.progressThickness = 0.2
progress.trackThickness = 0.3
progress.clockwise = true
progress.gradientRotateSpeed = 10
progress.roundedCorners = false
progress.glowMode = .Forward
progress.glowAmount = 0.9
progress.setColors(UIColor.yellowColor())
progress.center = CGPoint(x: view.center.x, y: view.center.y + 25)
view.addSubview(progress)
}
#IBAction func Animate(sender: AnyObject) {
progress.angle = Double(scorestart * anglepercent)
progress.animateFromAngle(0, toAngle: 270, duration: 2) {
completed in
if completed {
print("animation stopped, completed")
} else {
print("animation stopped, was interrupted")
}
Use :-
Basic idea here is that whenever your view will load corresponding class will look up to viewWillAppear(animated: Bool) function and if it's present in the code it will execute all the code in it.The moment that particular view is about to appear on your UI, your code block in viewWillAppear(animated: Bool) will get called.
class ViewController: UIViewController {
var progress: KDCircularProgress!
#IBOutlet weak var Label1: UILabel!
var LabelText = String()
var scorestart = 1.0
var anglepercent = 3.6
override func viewDidLoad() {
super.viewDidLoad()
Label1.text = LabelText
view.backgroundColor = UIColor(white: 0.22, alpha: 1)
}
override func viewWillAppear(animated :Bool) {
super.viewWillAppear(animated)
progressActn()
//Setting up your progress layer
animateActn()
//Animating that progress layer
}
#IBAction func Animate(sender: AnyObject) {
animateActn()
}
func animateActn(){
progress.angle = Double(scorestart * anglepercent)
progress.animateFromAngle(0, toAngle: 270, duration: 2) {
completed in
if completed {
print("animation stopped, completed")
} else {
print("animation stopped, was interrupted")
}
}
}
func progressActn(){
progress = KDCircularProgress(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
progress.startAngle = -90
progress.progressThickness = 0.2
progress.trackThickness = 0.3
progress.clockwise = true
progress.gradientRotateSpeed = 10
progress.roundedCorners = false
progress.glowMode = .Forward
progress.glowAmount = 0.9
progress.setColors(UIColor.yellowColor())
progress.center = CGPoint(x: view.center.x, y: view.center.y + 25)
view.addSubview(progress)
}
}

IOS8 and Swift multiple views with gestures dont work

I'm trying to build a user interface based on generic classes which are based on the same extension of the UIViewController class. This example puts squares on the screen and the tap event would be dealt with in the parent class, but it doesnt work. What am I doing wrong?
Main ViewController.swift:
class ViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//FIRST SQUARE
let widget1 = TestWidget()
widget1.viewX = 10
widget1.viewY = 10
self.view.addSubview(widget1.view)
self.addChildViewController(widget1)
widget1.didMoveToParentViewController(self)
//SECOND SQUARE
let widget2 = TestWidget()
widget2.viewX = 100
widget2.viewY = 100
self.view.addSubview(widget2.view)
self.addChildViewController(widget2)
widget2.didMoveToParentViewController(self)
}
}
Here is the TestWidget class:
class TestWidget : UIViewController {
var viewX : CGFloat!
var viewY : CGFloat!
override func viewDidLoad() {
super.viewDidLoad()
let square = UIView()
square.frame = CGRect(x : viewX, y : viewY, width: 50, height: 50)
square.backgroundColor = UIColor.orangeColor()
self.view.addSubview(square)
//GESTURE PART
var tapForSquare = UITapGestureRecognizer()
tapForSquare.addTarget(self, action : "onTap")
square.addGestureRecognizer(tapForSquare)
square.userInteractionEnabled = true
square.multipleTouchEnabled = true
}
func onTap() {
println("square tapped")
}
}
I have both squares on the screen but the tap action only works on the second one. Is there any problem with this?
onTap() takes an argument. It should be:
onTap(gestureRecognizer: UITapGestureRecognizer)
and add a colon after "onTop" in this line:
tapForSquare.addTarget(self, action : "onTap")
Thanks everyone, it seems interestingly this works only if you set the parent views frame as well.
So something like:
override func viewDidLoad() {
super.viewDidLoad()
self.view.frame = CGRect(x : viewX, y : viewY, width: 50, height: 50)
let square = UIView()
square.frame = CGRect(x : 0, y : 0, width: 50, height: 50)
square.backgroundColor = UIColor.orangeColor()
self.view.addSubview(square)
...
}

Resources