When I try to change color or tool nothing happens and it still drawing with deafult black pen.
class ViewController: UIViewController {
#IBOutlet weak var canvasView: PKCanvasView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setupCanvasView()
}
private func setupCanvasView() {
canvasView.drawingPolicy = .anyInput
let toolPicker = PKToolPicker()
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
canvasView.becomeFirstResponder()
}
}
Can't fix this problem. What should I do?
Related
In iOS Simulator, I've followed the basic steps to include PencilKit into my view controller. I got the toolKit in the UI but couldn't draw.. Not sure what I'm missing here. Sharing my code which I tried.
import UIKit
import PencilKit
class DrawingBoardViewController: UIViewController {
#IBOutlet weak var canvasView: PKCanvasView!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setupCanvasView()
}
func setupCanvasView() {
let toolPicker = PKToolPicker.init()
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
toolPicker.isRulerActive = false
canvasView.isOpaque = true
canvasView.becomeFirstResponder()
canvasView.drawingPolicy = .anyInput
}
}
Kindly let me know what I'm missing here..
This should fix your issue:
import UIKit
import PencilKit
class DrawingBoardViewController: UIViewController {
#IBOutlet weak var canvasView: PKCanvasView!
let toolPicker = PKToolPicker.init()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setupCanvasView()
}
func setupCanvasView() {
toolPicker.setVisible(true, forFirstResponder: canvasView)
toolPicker.addObserver(canvasView)
toolPicker.isRulerActive = false
canvasView.isOpaque = true
canvasView.becomeFirstResponder()
canvasView.drawingPolicy = .anyInput
}
}
I am creating an app in which I am using ARSKView. I am seeing that it is not releasing memory on dismissing ViewController. I am not able to find any issue in my code.
Here is my code for using ARSKView.
class ARViewController: UIViewController, ARSKViewDelegate {
weak var sceneView: ARSKView?
override func viewDidLoad() {
super.viewDidLoad()
sceneView = ARSKView(frame: self.view.frame)
self.view.addSubview(sceneView!)
sceneView?.delegate = self
let scene = CustomScene(size: view.frame.size)
scene.sceneDelegate = self
sceneView?.presentScene(scene)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
activateARView()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView?.session.pause()
}
var configuration = ARWorldTrackingConfiguration()
func activateARView() {
configuration.worldAlignment = .gravityAndHeading
sceneView?.session.run(configuration)
}
}
The controller is just paused, you can try by setting the controller equal to "nil" just make sure all dependencies are cleared before that.
I want to update the layout of a popover, I've already tried using the setNeedsLayout() and layoutIfNeeded() methods but couldn't find a solution.
I have a simple screen with 4 buttons that call a popover, within the popover there are 2 extra buttons:
Button -> hides button3, and moves button1 and button2 to the left.
reposition -> changes the sourceview of the popover from the button1/2/3/4 (sender) to button4.
Picture of the layout
The problem I have is that I'm not able to update/refresh the screen, and the popover keeps pointing to the former source view position.
Picture of the popover
Somehow I need a method that updates my view and which is stronger than layoutIfNeeded() because if the screen is changed from portrait/landscape the popover changes automatically. Here I changed portrait/landscape/portrait
GitHub of the project. GitHub
The code used is the following:
class ViewController: UIViewController, ButtonDidDisappearDelegate {
#IBOutlet weak var button1: UIButton!
#IBOutlet weak var button2: UIButton!
#IBOutlet weak var button3: UIButton!
#IBOutlet weak var button4: UIButton!
#IBOutlet weak var constrain2: NSLayoutConstraint!
var popVC: PopVC?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func buttonPopover(_ sender: UIButton) {
configureAndPresentPopover(from: sender)
}
func buttonDisappear() {
self.constrain2.constant = 100
self.button3.isHidden = !self.button3.isHidden
if !self.button3.isHidden {
self.constrain2.constant = 10
}
}
func repositionPopover() {
DispatchQueue.main.async {
if let popVC = self.popVC {
self.self.popoverPresentationController(popVC.popoverPresentationController!, willRepositionPopoverTo: self.button4.bounds, in: self.button4)
}
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
}
}
extension ViewController: UIPopoverPresentationControllerDelegate {
func configureAndPresentPopover(from sender: UIButton) {
popVC = storyboard?.instantiateViewController(withIdentifier: "popVC") as? PopVC
guard let popVC = popVC else {
return
}
popVC.popOverDelegate = self
popVC.modalPresentationStyle = .popover
let popOverVC = popVC.popoverPresentationController
popOverVC?.delegate = self
popOverVC?.sourceView = sender
popOverVC?.sourceRect = sender.bounds
popVC.preferredContentSize = CGSize(width: 300, height: 60)
popOverVC?.permittedArrowDirections = .down
self.present(popVC, animated: true)
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController,
willRepositionPopoverTo rect: CGRect,
in view: UIView) {
popoverPresentationController.sourceView = view
popoverPresentationController.sourceRect = rect
}
}
and the popover controller:
protocol ButtonDidDisappearDelegate: class {
func configureAndPresentPopover(from sender: UIButton)
func buttonDisappear()
func repositionPopover()
}
class PopVC: UIViewController {
weak var popOverDelegate: ButtonDidDisappearDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBOutlet weak var popoverButton: UIButton!
#IBAction func popoverAction(_ sender: Any) {
popOverDelegate?.buttonDisappear()
DispatchQueue.main.async {
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
}
}
#IBAction func reposition(_ sender: Any) {
popOverDelegate?.repositionPopover()
}
}
You need to specify the new position of the arrow and ask for the layout of the presentation controller's view, not only the presented view.
Try :
func repositionPopover() {
let popOverController = presentedViewController?.presentationController as? UIPopoverPresentationController
popOverController?.sourceView = button4
popOverController?.containerView?.setNeedsLayout()
popOverController?.containerView?.layoutIfNeeded()
}
You can try setting the sourceview and sourcerect once again. call this function when the sourcview bounds are changed.
How can I stop the video background when I move to another UIViewController?
because when I move to another UIViewController, the video and background music continue to be played, and when I go back to the main page two videos overlap. so I would like the video and the music to stop when I move to another UIViewController, such as when I move in the UIViewController for the sign up
import UIKit
import SwiftVideoBackground
import Firebase
import FirebaseAuth
class ViewController: UIViewController {
private let videoBackground = VideoBackground()
#IBOutlet weak var usernameField: UITextField!
#IBOutlet weak var passwordField: UITextField!
#IBOutlet weak var mute_img: UIImageView!
#IBOutlet private var muteSwitch: UISwitch!
#IBAction func `switch`(_ sender: UISwitch) {
if (sender.isOn == true)
{
mute_img.isHidden = false
videoBackground.isMuted = true
}
else
{
mute_img.isHidden = true
videoBackground.isMuted = false
}
let shouldMute = sender.isOn
videoBackground.isMuted = shouldMute
UserDefaults.standard.set(shouldMute, forKey:"isMuted")
}
override func viewDidLoad() {
super.viewDidLoad()
let userDefaults = UserDefaults.standard
let shouldMute = userDefaults.bool(forKey: "isMuted")
videoBackground.play(view: view, videoName: "intro", videoType:
"mp4", isMuted: shouldMute, willLoopVideo : true)
muteSwitch.isOn = shouldMute
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == usernameField {
passwordField.becomeFirstResponder()
} else if textField == passwordField {
textField.resignFirstResponder()
}
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Forward navigation :
videoBackground.pause()
Back navigation :
videoBackground.resume()
As per the Documentation:
You can simply use the pause()
When leaving the Controller
videoBackground.pause()
When Back to Controller:
videoBackground.resume()
I find a lot of resources on how to hide the navigation bar on scroll, but I would like to have the navigation bar hidden on start, and then appear when starting to scroll. Like this animation from Design+Code app: https://imgur.com/a/SqDRD
You can use UIScrollViewDelegate for that.
Here is example code for hide navigation bar and tool bar with scroll:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var toolBar: UIToolbar!
#IBOutlet weak var webV: UIWebView!
var lastOffsetY :CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
webV.scrollView.delegate = self
let url = "http://apple.com"
let requestURL = NSURL(string:url)
let request = NSURLRequest(URL: requestURL!)
webV.loadRequest(request)
}
//Delegate Methods
func scrollViewWillBeginDragging(scrollView: UIScrollView){
lastOffsetY = scrollView.contentOffset.y
}
func scrollViewWillBeginDecelerating(scrollView: UIScrollView){
let hide = scrollView.contentOffset.y > self.lastOffsetY
self.navigationController?.setNavigationBarHidden(hide, animated: true)
toolBar.hidden = hide
}
}
Using the willBeginDragging and didEndDragging you can accomplish what you want. Here there is a simplified version of it, you may need to modify it a little bit to obtain the desired effect you want, but it is an starting point.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.setNavigationBarHidden(true, animated: false)
}
}
extension ViewController: UIScrollViewDelegate {
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
navigationController?.setNavigationBarHidden(false, animated: true)
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
navigationController?.setNavigationBarHidden(true, animated: true)
}
}