Saving NSUserDefaults with UISwitch in Swift 2 - nsuserdefaults

I have posted all my code below to fully demonstrate what I am trying to accomplish. I have a UI Switch, that I would like to save a NSUserDefault on what game mode they would like. I am not sure why this is not working correctly, as it currently does not save- as the switch doesn't work.
import SpriteKit
class ModeScene: SKScene {
var modeSwitch = UISwitch()
var motionLabel = UILabel()
var touchLabel = UILabel()
override func didMoveToView(view: SKView) {
let MotionMode = NSUserDefaults.standardUserDefaults()
let TouchMode = NSUserDefaults.standardUserDefaults()
modeSwitch.center = CGPoint(x: view.center.x, y: view.frame.size.height / 1.3)
modeSwitch.addTarget(self, action: Selector("updateMode"), forControlEvents: UIControlEvents.ValueChanged)
if TouchMode {
modeSwitch.on = false
}
else{
modeSwitch.on = true
}
self.view?.addSubview(modeSwitch)
motionLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 700, height: 200))
motionLabel.center = CGPoint(x: view.center.x * 3.4, y: view.frame.size.height / 1.3)
motionLabel.text = "Accelerometer"
motionLabel.textColor = UIColor.whiteColor()
motionLabel.font = UIFont(name:"ArialRoundedMTBold", size: 16)
self.view?.addSubview(motionLabel)
touchLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 700, height: 200))
touchLabel.center = CGPoint(x: view.center.x * 2.25, y: view.frame.size.height / 1.3)
touchLabel.text = "Touch To Move"
touchLabel.textColor = UIColor.whiteColor()
touchLabel.font = UIFont(name:"ArialRoundedMTBold", size: 16)
self.view?.addSubview(touchLabel)
DoneBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 400, height: 30))
DoneBtn.center = CGPoint(x: view.center.x * 1.7, y: view.frame.size.height / 14)
DoneBtn.setTitle("Done", forState: UIControlState.Normal)
DoneBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
DoneBtn.addTarget(self, action: Selector("done"), forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(DoneBtn)
updateMode()
}
func updateMode(){
if modeSwitch.on {
// mode is accel
let MotionMode = NSUserDefaults.standardUserDefaults()
MotionMode.setValue(true, forKey: "MotionMode")
NSLog("Mode is accel")
}
else{
// mode is touch
let TouchMode = NSUserDefaults.standardUserDefaults()
TouchMode.setObject(true, forKey: "TouchMode")
NSLog("Mode is touch")
}
}
func done(){
self.scene?.view?.presentScene(StartScene(size: self.scene!.size), transition: SKTransition.fadeWithColor(UIColor.blackColor(), duration: 1))
headerLabel.removeFromSuperview()
DoneBtn.removeFromSuperview()
modeSwitch.removeFromSuperview()
touchLabel.removeFromSuperview()
motionLabel.removeFromSuperview()
}
}
Code Update:
import SpriteKit
class ModeScene: SKScene {
var modeSwitch = UISwitch()
var motionLabel = UILabel()
var touchLabel = UILabel()
var DoneBtn : UIButton!
override func didMoveToView(view: SKView) {
backgroundColor = SKColor.blackColor()
modeSwitch.center = CGPoint(x: view.center.x, y: view.frame.size.height / 1.3)
modeSwitch.addTarget(self, action: Selector("updateMode"), forControlEvents: UIControlEvents.ValueChanged)
self.view?.addSubview(modeSwitch)
motionLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 700, height: 200))
motionLabel.center = CGPoint(x: view.center.x * 3.4, y: view.frame.size.height / 1.3)
motionLabel.text = "Accelerometer"
motionLabel.textColor = UIColor.whiteColor()
motionLabel.font = UIFont(name:"ArialRoundedMTBold", size: 16)
self.view?.addSubview(motionLabel)
touchLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 700, height: 200))
touchLabel.center = CGPoint(x: view.center.x * 2.25, y: view.frame.size.height / 1.3)
touchLabel.text = "Touch To Move"
touchLabel.textColor = UIColor.whiteColor()
touchLabel.font = UIFont(name:"ArialRoundedMTBold", size: 16)
self.view?.addSubview(touchLabel)
DoneBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 400, height: 30))
DoneBtn.center = CGPoint(x: view.center.x * 1.7, y: view.frame.size.height / 14)
DoneBtn.setTitle("Done", forState: UIControlState.Normal)
DoneBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
DoneBtn.addTarget(self, action: Selector("done"), forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(DoneBtn)
let defaults = NSUserDefaults.standardUserDefaults()
if let motionMode = defaults.objectForKey("motionMode") as? Bool {
if motionMode {
// true
modeSwitch.on = true
} else {
// false
modeSwitch.on = false
}
}
updateMode()
}
func updateMode(){
let defaults = NSUserDefaults.standardUserDefaults()
if modeSwitch.on {
// mode is accel
defaults.setBool(true, forKey: "motionMode")
NSLog("Mode is accel")
}
else{
// mode is touch
defaults.setBool(true, forKey: "touchMode")
NSLog("Mode is touch")
}
}
func done(){
self.scene?.view?.presentScene(StartScene(size: self.scene!.size), transition: SKTransition.fadeWithColor(UIColor.blackColor(), duration: 1))
headerLabel.removeFromSuperview()
DoneBtn.removeFromSuperview()
modeSwitch.removeFromSuperview()
touchLabel.removeFromSuperview()
motionLabel.removeFromSuperview()
}
}

You forgot to actually load the values from NSUserDefaults.
You set them with setValue and setObject, but you never load them back.
Use the valueForKey or objectForKey methods to retrieve your values.
let defaults = NSUserDefaults.standardUserDefaults()
let MotionMode = defaults.objectForKey("MotionMode") as! Bool
if MotionMode {
// true
} else {
// false
}
Even better with safe unwrapping:
let defaults = NSUserDefaults.standardUserDefaults()
if let MotionMode = defaults.objectForKey("MotionMode") as? Bool {
if MotionMode {
// true
} else {
// false
}
}
A few notes: variables should begin with a lowercase letter, so "MotionMode" should be "motionMode", etc. You could use the specialized object getters like boolForKey instead of the generic objectForKey which forces you to typecast to Bool. Of course it goes with setBool on the other side. Last: the variable you call "MotionMode" actually holds NSUserDefaults.standardUserDefaults(), so I called it "defaults" instead in my example.
Your question has several parts, but it's better to make a single post for a single question. So I've focused about the failing NSUserDefaults part because it was the most important. Using my answer, if your code still doesn't work because of the UI parts, then you should post a new different question about it to keep things clear.

Related

How to dispose from UIView subviews on one ViewController when navigating away from it

I'm starting to learn Swift and decided to build an app but without storyboard.
My SceneDelegate scene function instantiates a TabBarController
window = UIWindow(frame: UIScreen.main.bounds)
let tb = TabBarController()
window?.rootViewController = tb
window?.makeKeyAndVisible()
window?.windowScene = windowSceme
I have a TabBarController that extends from UITabBarController which pretty much styles the tab bar and sets it all up
For each item of the Tabbar I have a ViewController. For the purpose of this question I'm going to specify the first "Home".
In Home, which extends ViewController, I have the following
let homePageView = HomePageView()
override func viewWillLayoutSubviews() {
navigationController?.isNavigationBarHidden = true
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
homePageView.setupHomePage()
view.addSubview(homePageView)
}
override func viewWillDisappear(_ animated: Bool) {
homePageView.dispose()
}
The Controller is pretty much only in charge of calling whatever is going to be displayed on the screen within HomePageView(), the main component. This last, holds two more views. One carrousel HomePageCarrousel() and one header HomePageSocialUp(). This view instantiates the two latter referred to and sets the layout programmatically and adds them as subviews. It also includes a dispose function that sets the instantiated classes to nil such as --> Instantiate - homePageCarrousel = HomePageCarrousel() and the dispose function has homePageCarrousel = nil
That works perfectly fine but when I navigate away from the current view controller via the tab bar and navigate back to it, now I have two instances of HomePageCarrousel() and HomePageSocialUp() within HomeView
I'm probably holding a strong reference somewhere but I can't figure out when. Could someone point me to where should I look to debug it or what might be that is creating the issue.
I'm also providing the code for the two views duplicated in case the issue is there
HomePageSocialUp
class HomePageSocialUp: UIView {
let logo = UIImage(named: "LogoSmiles")
let socialImages: [UIImage] = [UIImage(named: "tripadvisor")!, UIImage(named: "instagram")!, UIImage(named: "facebook")!, UIImage(named: "linkedin")!]
func setupHeaderCircle() {
guard let insetTop = UIApplication.shared.keyWindow?.safeAreaInsets.top else {return}
let circlePath = UIBezierPath(arcCenter: CGPoint(x: UIScreen.main.bounds.width / 2, y: insetTop + 60), radius: CGFloat(90), startAngle: CGFloat(0), endAngle: CGFloat(Double.pi * 2), clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
shapeLayer.fillColor = UIColor.white.cgColor
layer.addSublayer(shapeLayer)
}
func setupSocialHeader() {
setupHeaderCircle()
layer.masksToBounds = true;
backgroundColor = UIColor.TintColor
layer.shadowColor = UIColor.lightGray.cgColor
layer.shadowOpacity = 0.8
layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
layer.shadowRadius = 3.0
layer.masksToBounds = false
frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 280)
let imageView = UIImageView()
guard let logo = logo else {return}
guard let insetTop = UIApplication.shared.keyWindow?.safeAreaInsets.top else {return}
imageView.frame = CGRect(x: CGFloat(Int((UIScreen.main.bounds.width - (logo.size.width)))) / 2, y: 120 - (logo.size.width / 2), width: logo.size.width, height: logo.size.height)
imageView.image = logo
addSubview(imageView)
}
}
HomePageCarrousel
class HomePageCarrousel: UIScrollView {
var images: [UIImage]?
var originX = 0
var numberOfIterations = 0
var timer: Timer?
func setupCarrousel() {
let x = (Int(UIScreen.main.bounds.width) - (Int(UIScreen.main.bounds.width) - 60)) / 2
frame = CGRect(x: x, y: (Int(frame.origin.y) - 350) / 2, width: Int(UIScreen.main.bounds.width) - 60, height: 350)
let newImage = textToImage(drawText: "Creating Smiles in unique places.", frame: frame, inImage: UIImage(named: "smiles1")!, atPoint: CGPoint(x: frame.origin.x + 20, y: frame.height - 20))
images = [newImage, UIImage(named: "smiles2")!]
guard timer == nil else { return }
timer = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(startScrolling), userInfo: nil, repeats: true)
guard let imageCount = images?.count, let images = images else { return }
contentSize = CGSize(width: frame.width * CGFloat(images.count), height: frame.height)
for image in 0..<imageCount {
let imageView = UIImageView()
imageView.image = images[image]
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
let xPosition = frame.width * CGFloat(image)
imageView.frame = CGRect(x: xPosition, y: 0, width: frame.width, height: frame.height)
addSubview(imageView)
}
timer?.fire()
}
func textToImage(drawText text: String, frame: CGRect, inImage image: UIImage, atPoint point: CGPoint) -> UIImage {
let textColor = UIColor.white
let textFont = UIFont(name: "Helvetica Bold" , size: 12)!
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(image.size, false, scale)
let textFontAttributes = [
NSAttributedString.Key.font: textFont,
NSAttributedString.Key.foregroundColor: textColor,
] as [NSAttributedString.Key : Any]
image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))
text.draw(in: frame, withAttributes: textFontAttributes)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
#objc func startScrolling() {
print(originX)
guard let images = images else { return }
if originX == images.count {
originX = 0
numberOfIterations += 1
}
if numberOfIterations > 2 {
timer?.invalidate()
timer = nil
numberOfIterations = 0
}
let x = CGFloat(originX) * frame.size.width
setContentOffset(CGPoint(x: x, y: 0), animated: true)
originX += 1
}
}
Thanks upfront
Did you tried to remove the subviews, something like
homePageView.subviews.forEach { (view) in
//taking appropriate action to whichever view you want
view.removeFromSuperview()//for removing views
}

tap event does not fire on animated imageview

I want an animating imageView with UIBezierPath (sliding from top to bottom) to be clicked. But every google help I found didn't solve it. But it does not print "clicked".
The animation does work perfectly. Maybe anyone's got a clue?
Help is very appreciated.
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView(image: UIImage(named: "advent_button"))
let dimension = 25 + drand48() * 30
imageView.frame = CGRect(x: 0, y: 0, width: dimension, height: dimension)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleImageTap))
imageView.addGestureRecognizer(tapGestureRecognizer)
imageView.isUserInteractionEnabled = true
let animation = CAKeyframeAnimation(keyPath: "position")
animation.path = customPath().cgPath
animation.duration = 20
animation.fillMode = .forwards
animation.isRemovedOnCompletion = false
animation.repeatCount = 0.5
animation.timingFunction = CAMediaTimingFunction(name: .easeOut)
imageView.layer.add(animation, forKey: nil)
view.addSubview(imageView)
}
#objc func handleImageTap() {
print ("clicked")
}
func customPath() -> UIBezierPath {
let path = UIBezierPath()
path.move(to: CGPoint(x: 200, y: 0))
let endpoint = CGPoint(x: 20 , y: 700)
let cp1 = CGPoint(x: 90, y: 250)
let cp2 = CGPoint(x: 180, y: 400)
path.addCurve(to: endpoint, controlPoint1: cp1, controlPoint2: cp2)
return path
}
I've tried the following:
UIView.animate(withDuration: 30, delay: delay, options: [.allowUserInteraction], animations: {
self.button.frame = CGRect(
x: randomXEndShift,
y: 700,
width: dimension,
height: dimension)
}, completion: nil)
Shows the same behavoir. Even using .allowUserInteraction does not help at all.
I got your issue. i am sure you are touching layer not UIImageView and you have added uitapgesturerecognizer to UIImageview not layer. Do one thing set
animation.isRemovedOnCompletion = true
and set imageView.frame similar co- ordinates where animation will stop
Full code :
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView(image: UIImage(named: "Icon123"))
imageView.backgroundColor = .red
// let dimension = 25 + drand48() * 30
imageView.isUserInteractionEnabled = true
imageView.frame = CGRect(x: 10, y: 200, width: 50, height: 50)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleImageTap))
imageView.addGestureRecognizer(tapGestureRecognizer)
let animation = CAKeyframeAnimation(keyPath: "position")
animation.path = customPath().cgPath
animation.duration = 5
animation.fillMode = .forwards
animation.isRemovedOnCompletion = true
animation.repeatCount = 0.5
animation.timingFunction = CAMediaTimingFunction(name: .easeOut)
imageView.layer.add(animation, forKey: nil)
view.addSubview(imageView)
// Do any additional setup after loading the view, typically from a nib.
}
#objc func handleImageTap() {
print ("clicked")
}
func customPath() -> UIBezierPath {
let path = UIBezierPath()
path.move(to: CGPoint(x: 200, y: 0))
let endpoint = CGPoint(x: 20 , y: 700)
let cp1 = CGPoint(x: 90, y: 250)
let cp2 = CGPoint(x: 180, y: 400)
path.addCurve(to: endpoint, controlPoint1: cp1, controlPoint2: cp2)
return path
}
}

Circular crop for photos from photo library when using UIImagePickerController

I am trying to change the square crop tool to circular one for the images selected from Photo Library in Swift 4. I tried many old codes available in here with no luck. Can somebody please help me.
There are mainly 2 issues in this code.
It doesn't hide the already existing square cropping area.
It shows the choose and cancel buttons under the old overlay, so cant tap on it.
Any help would be appreciated.
My code:
private func hideDefaultEditOverlay(view: UIView)
{
for subview in view.subviews
{
if let cropOverlay = NSClassFromString("PLCropOverlayCropView")
{
if subview.isKind(of: cropOverlay) {
subview.isHidden = true
break
}
else {
hideDefaultEditOverlay(view: subview)
}
}
}
}
private func addCircleOverlayToImageViewer(viewController: UIViewController) {
let circleColor = UIColor.clear
let maskColor = UIColor.black.withAlphaComponent(0.8)
let screenHeight = UIScreen.main.bounds.size.height
let screenWidth = UIScreen.main.bounds.size.width
hideDefaultEditOverlay(view: viewController.view)
let circleLayer = CAShapeLayer()
let circlePath = UIBezierPath(ovalIn: CGRect(x: 0, y: screenHeight - screenWidth, width: screenWidth, height: screenWidth))
circlePath.usesEvenOddFillRule = true
circleLayer.path = circlePath.cgPath
circleLayer.fillColor = circleColor.cgColor
let maskPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight), cornerRadius: 0)
maskPath.append(circlePath)
maskPath.usesEvenOddFillRule = true
let maskLayer = CAShapeLayer()
maskLayer.path = maskPath.cgPath
maskLayer.fillRule = kCAFillRuleEvenOdd
maskLayer.fillColor = maskColor.cgColor
viewController.view.layer.addSublayer(maskLayer)
// Move and Scale label
let label = UILabel(frame: CGRect(x: 0, y: 20, width: view.frame.width, height: 50))
label.text = "Move and Scale"
label.textAlignment = .center
label.textColor = UIColor.white
viewController.view.addSubview(label)
}

Customized UITextField in Swift 4.0 text overlapped when edit

I am trying to make a textField for username and password.
So far, this is what I have achieved:
Current View on iPhone
However, when editing, everything is good. But then I try to edit again, this happened.
The previous text shifted place back to the origin
Here are the code for the textfield
#IBDesignable class StandardInputTextField: UITextField {
let kInset = Constants.buttonTitleInset
#IBInspectable var leftImage: UIImage? {
didSet {
updateView()
}
}
#IBInspectable var leftPadding: CGFloat = 0 {
didSet {
updateView()
}
}
fileprivate func updateView() {
layer.borderColor = UIColor.clear.cgColor
if let leftImage = leftImage {
leftViewMode = .always
let imageView = UIImageView(image: leftImage)
if frame.height >= 20 {
imageView.frame = CGRect(x: kInset,
y: kInset / 2.0,
width: frame.height - 2 * kInset,
height: frame.height - 2 * kInset)
} else {
imageView.frame = CGRect(x: 0, y: 0,
width: frame.height,
height: frame.height)
}
var containerWidth =
imageView.frame.width + 2 * kInset
if borderStyle == .none ||
borderStyle == .line {
containerWidth += 5
}
let imageViewContainer = UIView(frame:
CGRect(x: 0, y: 0,
width: containerWidth,
height: imageView.frame.height + 2 * kInset))
imageViewContainer.addSubview(imageView)
leftView = imageViewContainer
} else {
leftViewMode = .never
}
}
override func draw(_ rect: CGRect) {
super.drawText(in: rect)
if leftViewMode == .always {
guard let leftViewRect = leftView?.frame else {
return
}
let linePath = UIBezierPath()
linePath.move(to:
CGPoint(x: leftViewRect.minX,y: rect.maxY))
linePath.addLine(to:
CGPoint(x: rect.maxX, y: rect.maxY))
Colors.ButtonColor.set()
linePath.lineWidth = 2.0
linePath.stroke()
}
}
}
So did this happen to anyone? and how to fix this? Much appreciated.
Apparently, this happens when I do this :
override func draw(_ rect: CGRect) {
super.draw(rect)
}
So the solution is simply remove
super.draw(rect)
That's it, enjoy

How to make this UIButton's action operate correctly?

Every time I run this swift playground (on Xcode), I get an error on the last line saying:
'PlaygroundView' cannot be constructed because it has no accessible initialisers.
I also have another error on line 4 saying:
class 'PlaygroundView' has no initializers.
import UIKit
import PlaygroundSupport
class PlaygroundView:UIViewController {
var introImage:UIImageView
var introButton:UIButton
override func loadView() {
print("workspace started running.")
//Main Constant and Variable Declarations/General Setup
let mainView = UIView()
//Main View Modifications & styling
mainView.backgroundColor = UIColor.init(colorLiteralRed: 250, green: 250, blue: 250, alpha: 1)
func addItem(item: UIView) {
mainView.addSubview(item)
}
//Sub-Element Constant and Variable Declarations
introImage = UIImageView(frame: CGRect(x: 87.5, y: -300, width: 200, height: 200))
introImage.layer.borderColor = UIColor.black.cgColor
introImage.layer.borderWidth = 4
introImage.alpha = 0
introImage.transform = CGAffineTransform(rotationAngle: -90.0 * 3.14/180.0)
introImage.image = UIImage(named: "profile_pic.png")
introImage.image?.accessibilityFrame = introImage.frame
introImage.layer.cornerRadius = 100
addItem(item: introImage)
introButton = UIButton(frame: CGRect(x: 87.5, y: 900, width: 200, height: 30))
introButton.alpha = 0
introButton.setTitle("Tap to meet me", for: .normal)
introButton.titleLabel?.font = UIFont(name: "Avenir Book", size: 20)
introButton.backgroundColor = UIColor.black
introButton.layer.cornerRadius = 15
introButton.layer.borderWidth = 5
addItem(item: introButton)
introButton.addTarget(self, action: #selector(self.introButtonAction), for: .touchDown)
UIView.animate(withDuration: 1.5, delay: 1, options: .curveEaseInOut, animations: {
self.introImage.frame = CGRect(x: 87.5, y: 175, width: 200, height: 200)
self.introButton.frame = CGRect(x: 87.5, y: 400, width: 200, height: 30)
self.introImage.transform = CGAffineTransform(rotationAngle: 0.0 * 3.14/180.0)
self.introImage.alpha = 1
self.introButton.alpha = 1
}) { (mainAnimationComped) in
if mainAnimationComped == true {
print("introduction animation comped.")
}
}
}
//User Interface Actions
func introButtonAction() {
print("user interacted with user interface")
}
}
PlaygroundPage.current.liveView = PlaygroundView()
How would I fix this problem?
That's because in Swift, you need to initialize variables before use them in the code. On the other hand, since in this case you set them explicitly in your function, you might declare them as implicitly unwrapped optionals
var introImage:UIImageView!
var introButton:UIButton!
and this should work without changing anything else in your code
Add ? after introImage and introButton
var introImage:UIImageView?
var introButton:UIButton?