iCarousel not scrolling SpriteKit in Swift - ios

I'm trying to use an iCarousel in an SKScene. The problem is that when I drag on the carousel it doesn't change the images. I haven't found a tutorial for iCarousel in SpriteKit so a lot of this was guesswork.
This is my code:
import UIKit
import SpriteKit
class Hobbies: SKScene, iCarouselDelegate, iCarouselDataSource {
var carousel : iCarousel = iCarousel()
var imageArray : NSMutableArray = NSMutableArray()
override func didMove(to view: SKView) {
carousel.delegate = self
carousel.dataSource = self
carousel.type = .cylinder
carousel.center = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
carousel.isUserInteractionEnabled = true
carousel.reloadData()
self.view?.addSubview(carousel)
}
func numberOfItems(in carousel: iCarousel) -> Int {
imageArray = ["Hobbies1.png", "Hobbies2.png", "Hobbies3.png"]
return imageArray.count
}
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
var imageview : UIImageView!
if view == nil {
imageview = UIImageView(frame: CGRect(x: 0, y: 0, width: 250, height: 250))
imageview.contentMode = .scaleAspectFit
} else {
imageview = view as! UIImageView
}
imageview.image = UIImage(named: "\(imageArray.object(at: index))")
return imageview
}
func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
if option == iCarouselOption.spacing {
return value * 1.2
}
return value
}
}
Screenshot:
The iCarousel is appearing but won't scroll.
What am I missing?
The scrolling works when using autoscroll but not with normal scroll.

Related

How do I make new image views movable?

I have been trying to piece together a project that contains a collection view that scrolls on the side, and when a cell is tapped it will add a new image view to the scene. I would like this new image to be draggable.
My code currently shows the collection view and when tapped adds a new image (which I call stickers).
I haven't quite figured out how to make it place the correct sticker yet, but my first goal is to make sure what appears can be moved.
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
#IBOutlet weak var collectionView: UICollectionView!
let stickers: [UIImage] = [
UIImage(named: "cow")!,
UIImage(named: "chicken")!,
UIImage(named: "pig")!,
UIImage(named: "cow")!,
UIImage(named: "chicken")!,
UIImage(named: "pig")!,
UIImage(named: "cow")!,
UIImage(named: "chicken")!,
UIImage(named: "pig")!,
]
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return stickers.count
}
var activeSticker = UIImage(named: "cow")
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
cell.stickerImage.image = stickers[indexPath.item]
cell.backgroundColor = UIColor(white: 1, alpha: 0.9)
cell.translatesAutoresizingMaskIntoConstraints = false
cell.contentMode = .scaleAspectFill
cell.clipsToBounds = true
cell.layer.cornerRadius = 7
let addSticker = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
addSticker.addTarget(self, action: #selector(addStickerTapped), for: UIControl.Event.touchUpInside)
activeSticker = cell.stickerImage.image
cell.addSubview(addSticker)
return cell
}
#IBAction func addStickerTapped() -> Void {
print("Hello Sticker Button")
let image = activeSticker //UIImage(named: imageName)
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 100, y: 100, width: 100, height: 200)
imageView.contentMode = .scaleAspectFit
imageView.isUserInteractionEnabled = true
self.view.addSubview(imageView)
//Imageview on Top of View
self.view.bringSubviewToFront(imageView)
}
}
To move the imageView around inside self.view you can use UIPanGestureRecognizer. Add the gesture recognizer, for example, in viewDidLoad:
class ViewController: UIViewController {
var selectedImageView: UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
addPanGestureRecognizer()
}
func addPanGestureRecognizer() {
let pan = UIPanGestureRecognizer(target: self, action: #selector(moveImageView(_:)))
// set up and optimize pan gesture options here if you need to
self.view.addGestureRecognizer(pan)
}
#objc func moveImageView(_ sender: UIPanGestureRecognizer) {
// assign one of your image views to selectedImageView to ensure you only move one image view at a time
// for example in func addStickerTapped() you could assign selectedImageView = imageView
guard let selectedImageView = selectedImageView else {
return
}
switch sender.state {
case .changed, .ended:
selectedImageView.center = selectedImageView.center.offset(by: sender.translation(in: self.view))
sender.setTranslation(.zero, in: self.view)
default:
break
}
}
}
extension CGPoint {
func offset(by point: CGPoint) -> CGPoint {
return CGPoint(x: self.x + point.x, y: self.y + point.y)
}
}

UIButton not showing when UIScrollView showing on ViewController

I have a UIViewController that is attached to a class, Swift code below. The code basically adds to the View Controller a UIScrollView with an image.
I am wanting to add a UIButton to the ViewController which I could do through code, however in this case, I want to add the UIButton to the ViewController using the Storyboard.
When I add a UIButton and then run my project, the UIButton is not visible, only the UIScrollView is visible.
Question:
What is going on, why is the UIButton not visible? How can I add a UIButton (to the Storyboard) and ensure that it is visible and in front of the UIScrollView (that is created programatically) when I run the project?
class ScrollViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView = UIImageView(image: UIImage(named: "image.png"))
scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.blackColor()
scrollView.contentSize = imageView.bounds.size
scrollView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
scrollView.contentOffset = CGPoint(x: 1000, y: 450)
scrollView.addSubview(imageView)
view.addSubview(scrollView)
scrollView.delegate = self
setZoomScale()
setupGestureRecognizer()
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
override func viewWillLayoutSubviews() {
setZoomScale()
}
func setZoomScale() {
let imageViewSize = imageView.bounds.size
let scrollViewSize = scrollView.bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height
scrollView.minimumZoomScale = min(widthScale, heightScale)
scrollView.zoomScale = 1.0
}
func scrollViewDidZoom(scrollView: UIScrollView) {
let imageViewSize = imageView.frame.size
let scrollViewSize = scrollView.bounds.size
let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0
scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}
func setupGestureRecognizer() {
let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
doubleTap.numberOfTapsRequired = 2
scrollView.addGestureRecognizer(doubleTap)
}
func handleDoubleTap(recognizer: UITapGestureRecognizer) {
if (scrollView.zoomScale > scrollView.minimumZoomScale) {
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
} else {
scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
}
}
}
Your scrollview is blocking the UIButton on z axis, please use
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
to insert scrollview below UIButton
Swift:
func insertSubview(view: UIView, atIndex index: Int) {
}
Answer:
Instead of this line view.addSubview(scrollView), you need to do self.view.insertSubview(scrollView, atIndex: 0) and make sure UIButton is above the scrollView.
You have to write:
self.view.insertSubview(scrollview, belowSubview: button)

scrollViewDidEndZooming not called while zooming in a UIScrollView

This could possibly be an Xcode bug? I have an UIImage inside a UIScrollView done programatically in code. The user can zoom into the image using the zoom gesture or double tapping the image. This part all works fine.
Next, I am trying to detect when the scroll view has finished zooming after a zoom gesture or double tap using the below function scrollViewDidEndZooming. However, scrollViewDidEndZooming never gets called, and doesn't print the line I just finished zooming!
Code not working:
func scrollViewDidEndZooming(scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
print("I just finished zooming!")
}
Question:
What might be going on here, why isn't scrollViewDidEndZooming(...)
getting called?
Note: I've referred to the below document with no luck.
https://developer.apple.com/reference/uikit/uiscrollviewdelegate/1619407-scrollviewdidendzooming
Complete code:
class ScrollViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView = UIImageView(image: UIImage(named: "image.png"))
scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.blackColor()
scrollView.contentSize = imageView.bounds.size
scrollView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
scrollView.contentOffset = CGPoint(x: 1000, y: 450)
scrollView.addSubview(imageView)
view.addSubview(scrollView)
scrollView.delegate = self
setZoomScale()
setupGestureRecognizer()
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
override func viewWillLayoutSubviews() {
setZoomScale()
}
func setZoomScale() {
let imageViewSize = imageView.bounds.size
let scrollViewSize = scrollView.bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height
scrollView.minimumZoomScale = min(widthScale, heightScale)
scrollView.zoomScale = 1.0
}
func scrollViewDidZoom(scrollView: UIScrollView) {
let imageViewSize = imageView.frame.size
let scrollViewSize = scrollView.bounds.size
let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0
scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}
func scrollViewDidEndZooming(scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
print("I just finished zooming!")
}
func setupGestureRecognizer() {
let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
doubleTap.numberOfTapsRequired = 2
scrollView.addGestureRecognizer(doubleTap)
}
func handleDoubleTap(recognizer: UITapGestureRecognizer) {
if (scrollView.zoomScale > scrollView.minimumZoomScale) {
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
} else {
scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
}
}
}
You make a small mistake. Try this code:
func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
print("I just finished zooming!")
}

Rotating UIView in UIScrollview Swift

When I rotate the view in the scrollview it moves out of the scrollview and disappears completely after some rotation/zoom gestures. It works fine as long as the zoom scale is 1.
What do I have to do with my code to avoid this?
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
let scrollView = UIScrollView(frame: UIScreen.mainScreen().bounds)
let rotationView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView()
view.addSubview(scrollView)
scrollView.delegate = self
scrollView.minimumZoomScale = 0.5
scrollView.maximumZoomScale = 2
let mapImage = UIImage(named: "BMS2_300.jpg")
imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size:mapImage!.size)
imageView.image = mapImage
let rotationViewframe = CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)
rotationView.frame = rotationViewframe
rotationView.addSubview(imageView)
//rotationView.sizeToFit()
scrollView.addSubview(rotationView)
scrollView.contentSize = CGSize(width: rotationView.bounds.width, height: rotationView.bounds.height)
scrollView.bringSubviewToFront(rotationView)
scrollView.contentOffset = CGPoint(x: rotationView.frame.width/2, y: rotationView.frame.height/2)
let mapRotGestureRecognizer = UIRotationGestureRecognizer(target: self, action: #selector(ViewController.rotateMap(_:)))
rotationView.addGestureRecognizer(mapRotGestureRecognizer)
}
func rotateMap(sender: UIRotationGestureRecognizer) {
let radians = sender.rotation
if let senderView = sender.view {
senderView.transform = CGAffineTransformRotate(senderView.transform, radians)
sender.rotation = 0
}
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return self.rotationView
}
func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
scrollView.contentSize = rotationView.frame.size
}
The solution is to add an extra uiview under the scrollview, so the new holderView is a subview of the scrollview and the rotatonView a subview of the holderView.

Programmatic implementation of iCarousel Swift example

Here is a Storyboard example of iCarousel implementation in Swift: https://github.com/nicklockwood/iCarousel/tree/master/Examples/Swift%20Example
Particularly this page:
https://github.com/nicklockwood/iCarousel/blob/master/Examples/Swift%20Example/SwiftExample/ViewController.swift
Can somebody guide me how to implement it programmatically (without Storyboards)?
swift 3
private let HEIGHT_CAROUSEL: CGFloat = 100
private func initiCarousel() {
let carousel = iCarousel(frame: CGRect(x: 0, y: view.frame.height - HEIGHT_CAROUSEL, width: view.frame.width, height: HEIGHT_CAROUSEL))
carousel.delegate = self
carousel.dataSource = self
view.addSubview(carousel)
}
// MARK: - iCarouselDelegate
extension YourNameViewController: iCarouselDataSource, iCarouselDelegate {
func numberOfItems(in carousel: iCarousel) -> Int {
return arrr.count ?? 0
}
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
var itemView: UIImageView
itemView = UIImageView(frame: CGRect(x: 0, y: 0, width: HEIGHT_CAROUSEL, height: HEIGHT_CAROUSEL))
itemView.contentMode = .scaleAspectFill
if let image = url {
itemView.setImageWithIndicator(imageUrl: image)
}
return itemView
}
func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
if (option == .spacing) {
return value * 1.1
}
return value
}
}
Use following code to implement iCarousel programmatically using swift:
override func viewDidLoad()
{
super.viewDidLoad()
carousel = iCarousel(frame: CGRectMake(0, 0, 200, 200))
carousel.center = view.center
carousel.dataSource = self
carousel.delegate = self
}

Resources