I would like to detect when the user scroll, I used a UIScrollView, I implemented the UIScrollViewDelegate in my ViewController and tried scrollViewDidScroll, scrollViewDidEndScrollingAnimation and all the others but these events are never called.
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let xOrigin = self.view.frame.width
let view1 = View1(frame: CGRect(x: 0, y: 0, width:self.view.frame.width, height: self.view.frame.height))
let view2 = View2(frame: CGRect(x: xOrigin, y: 0, width: self.view.frame.width, height: self.view.frame.height))
let view3 = View3(frame: CGRect(x: xOrigin * 2, y: 0, width: self.view.frame.width, height: self.view.frame.height))
scrollView.addSubview(view1)
scrollView.addSubview(view2)
scrollView.addSubview(view3)
self.scrollView.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.height)
// hide the scrol bar.
scrollView?.showsHorizontalScrollIndicator = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("end scroll")
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
print("end scroll")
}
}
You just have to add the delegate to your viewDidLoad() func:
override func viewDidLoad() {
super.viewDidLoad()
//add this line
self.scrollView.delegate = self
}
Related
I am following a tutorial to create a swift app that adds a number of views to a scroll view and then allows me to scroll between them. I have the app working and understand it for the most part. When I change the orientation of the device the views width don't get updated so I have parts of more than one view controller on the screen? Does anybody know how to fix this? From my understanding I need to call something in the viewsDidTransition method to redraw the views. Any help would be greatly appreciated. Thanks!
Here is what I have so far:
import UIKit
class ViewController: UIViewController {
private let scrollView = UIScrollView()
private let pageControl: UIPageControl = {
let pageControl = UIPageControl()
pageControl.numberOfPages = 5
pageControl.backgroundColor = .systemBlue
return pageControl
}()
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
pageControl.addTarget(self,
action: #selector(pageControlDidChange(_:)),
for: .valueChanged)
scrollView.backgroundColor = .red
view.addSubview(scrollView)
view.addSubview(pageControl)
}
#objc private func pageControlDidChange(_ sender: UIPageControl){
let current = sender.currentPage
scrollView.setContentOffset(CGPoint(x: CGFloat(current) * view.frame.size.width,
y: 0), animated: true)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
pageControl.frame = CGRect(x: 10, y: view.frame.size.height - 100, width: view.frame.size.width - 20, height: 70)
scrollView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height - 100)
if scrollView.subviews.count == 2 {
configureScrollView()
}
}
private func configureScrollView(){
scrollView.contentSize = CGSize(width: view.frame.size.width*5, height: scrollView.frame.size.height)
scrollView.isPagingEnabled = true
let colors: [UIColor] = [.systemRed, .systemGray, .systemGreen, .systemOrange, .systemPurple]
for x in 0..<5{
let page = UIView(frame: CGRect(x: CGFloat(x) * view.frame.size.width, y: 0, width: view.frame.size.width, height: scrollView.frame.size.height))
page.backgroundColor = colors[x]
scrollView.addSubview(page)
}
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
print("hello world")
}
}
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
pageControl.currentPage = Int(floorf(Float(scrollView.contentOffset.x) / Float(scrollView.frame.size.width)))
}
}
I have my Push button in subview called AddContactViewController
And I'm showing it in ScrollView in another ViewController called AddContactScrollViewController.
I'm setting action for my button in AddContactViewController
But, when I click my button nothing happens.
Here are the screenshots my IB. And below is the code.
Main view controller
SubView
import Cocoa
class AddContactScrollViewController: NSViewController {
#IBOutlet weak var scrollView: NSScrollView!
var contentView: NSView?
override func viewDidLoad() {
super.viewDidLoad()
contentView = NSView(frame: NSRect(x: 0, y: 0, width: self.view.frame.width, height: 1123))
contentView!.wantsLayer = true
contentView!.layer?.backgroundColor = NSColor.clear.cgColor
let tempVC = self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "addAddress")) as! AddAddressViewController
vc.view.setFrameOrigin(NSPoint(x: 0, y: 0))
vc.view.setFrameSize(NSSize(width: 1200, height: 1123))
vc.view.wantsLayer = true
contentView!.addSubview(vc.view)
scrollView.documentView = contentView
// scroll to the top
if let documentView = scrollView.documentView {
documentView.scroll(NSPoint(x: 0, y: documentView.bounds.size.height))
}
}
override func viewDidAppear() {
}
}
import Cocoa
class AddContactVeiwController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func addPhoneButtonClicked(_ sender: Any) {
print("phone button clicked")
}
I'm trying to understand how addArrangedSubview is working so I tried with a dummy project. Here's my VC :
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var stackOutlet: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let newView = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
newView.backgroundColor = UIColor.darkGray
let newView2 = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
newView2.backgroundColor = UIColor.blue
stackOutlet.addArrangedSubview(newView)
stackOutlet.addArrangedSubview(newView2)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
When I launch the app, nothing is appearing. I think I have to set some constraints in the stackView?
Ok so for everyone sakes : you need to use this =
mapView.heightAnchor.constraint(equalToConstant: 200).isActive = true
mapView.widthAnchor.constraint(equalToConstant: 200).isActive = true
I currently have a subclass of UIView which contains numerous subviews. I wish to add a UISwipeGesture to the subviews but unfortunately the swipe gesture is not recognized. I've set userInteractionEnabled = true and direction of the swipe gesture but nothing works.
public class CardStackView: UIView{
public var dataSource = [UIImage]()
private var swipeGuesture: UISwipeGestureRecognizer!
override public func layoutSubviews() {
for img in dataSource{
let view = AppView(image: img, frame: self.frame)
self.addSubview(view)
}
animateSubview()
self.userInteractionEnabled = true
}
func animateSubview(){
for (index, sView) in self.subviews.enumerate() {
swipeGuesture = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeGuestureDidSwipeRight(_:)))
swipeGuesture.direction = .Right
sView.addGestureRecognizer(swipeGuesture)
sView.userInteractionEnabled = true
let move: CGFloat = CGFloat(-20 + index * 20)
let opacity = Float(1 - 0.2 * CGFloat(index))
sView.shadowOpacity(opacity).shadowOffset(CGSizeMake(20 - CGFloat(index) * 5, 20 - CGFloat(index) * 5)).shadowRadius(5).moveX(-move).moveY(-move).gravity().shadowColor(UIColor.grayColor()).duration(1)
.completion({
}).animate()
}
}
func swipeGuestureDidSwipeRight(gesture: UISwipeGestureRecognizer) {
print("Swiped right")
let subview = self.subviews[0]
subview.moveX(-60).duration(1).animate()
}
}
Example
class ExampleController: UIViewController {
var stackView: CardStackView!
override func viewDidLoad() {
super.viewDidLoad()
stackView = CardStackView(frame: CGRect(x: 20, y: 80, width: 200, height: 200))
stackView.dataSource = [UIImage(named: "2008")!, UIImage(named: "2008")!]
self.view.addSubview(stackView)
}
}
self.view.bringSubviewToFront(yourSubview)
try this code for all your subviews and if it doesn't work try this in your controller class for your CardStackView.
Try to call setNeedsLayout for stackView:
override func viewDidLoad() {
super.viewDidLoad()
stackView = CardStackView(frame: CGRect(x: 20, y: 80, width: 200, height: 200))
stackView.dataSource = [UIImage(named: "2008")!, UIImage(named: "2008")!]
stackView.setNeedsLayout()
self.view.addSubview(stackView)
}
I tried this:
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var butt: NSButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
#IBAction func buttPressed(sender: AnyObject) {
let myPath = NSBezierPath()
myPath.moveToPoint(CGPoint(x: 20, y: 20))
myPath.lineToPoint(CGPoint(x: 100, y: 100))
myPath.stroke()
}
}
It compiles and runs but doesn't draw anything when the button is clicked.
I tried setting the color: NSColor.redColor().set(), setting the width: myPath.lineWidth = 20 and using NSPoint instead of CGPoint but still the same thing happens. What is it that I'm missing?
Thank you for your answers in advance and sorry for the elementary question.
Try doing it inside drawRect (NSView):
import Cocoa
final class Line: NSView {
override func drawRect(dirtyRect: NSRect) {
let myPath = NSBezierPath()
myPath.moveToPoint(CGPoint(x: 20, y: 20))
myPath.lineToPoint(CGPoint(x: 100, y: 100))
myPath.stroke()
}
}
final class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
let frame = CGRect(x: 0, y: 0, width: 100, height: 100)
let line = Line(frame: frame)
view.addSubview(line)
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
}