Move image to other view with double tap - ios

I am new to Swift and I am looking for a way to move my image to other view controller with double tap.
I made my image into scroll view, so I can slide to view it.
Here is my code.
class Quotes: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scroll: UIScrollView!
let imageview = ["quotes1","quotes2","quotes3","quotes4"]
var imagine = UIImageView()
override func viewDidLoad() {
let tap = UITapGestureRecognizer(target: self, action: #selector(doubletap))
tap.numberOfTapsRequired = 2
view.addGestureRecognizer(tap)
self.navigationController?.setNavigationBarHidden(true, animated: true)
quotesimageload()
}
func quotesimageload() {
for index in 0 ... imageview.count - 1
{
imagine = UIImageView (frame:CGRect(x: self.scroll.frame.width * CGFloat(index), y: 0 , width: self.scroll.frame.width, height: self.scroll.frame.height))
imagine.image = UIImage(named : imageview[index])
imagine.tag = index
imagine.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.scroll.addSubview(imagine)
}
self.scroll.contentSize = CGSize(width: self.scroll.frame.width * CGFloat(imageview.count), height: self.scroll.frame.height)
}
func doubletap(){
let view = self.storyboard?.instantiateViewController(withIdentifier: "pinch")
self.navigationController?.pushViewController(view!, animated: true)
}
}

You don't exactly move the image to another view controller - you pass a copy of it to the target VC.
Set up a "var" UIImage in the secondVC (let's say you called it image) and before you push that VC into view, populate it from the first VC.
One more word of caution - naming a UIViewController as "view" can be very confusing, as many would think it's a UIView. So assuming you rename that to be pinchViewController, the full syntax would be:
Second VC:
var image:UIView!
First VC:
func doubletap(){
let pinchViewController = self.storyboard?.instantiateViewController(withIdentifier: "pinch")
pinchViewController.image = imagine.image
self.navigationController?.pushViewController(view!, animated: true)
}
And if you have things properly coded and/or wired up in IB, you should have you UIImageView in the second VC displaying the image.

Related

Application stops after swiping

I have an app reader like Apple Books. And I have a problem. I have NavigationViewController next FirstViewController with book button, SecondViewController it is a reader and TableViewController it is settings for text size. But if I open book in FirstViewController go to SecondViewController and in navigation bar tab aA button and open TableViewController all works fine. But after if I want back to FirstViewController I not close TableViewController and do swipe from left to right my app stops. How to fix it?
SecondViewController:
#IBOutlet weak var textSettings: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
setupGestures()
}
// MARK: - Text Settings View
private func setupGestures() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapped))
tapGesture.numberOfTapsRequired = 1
textSettings.addGestureRecognizer(tapGesture)
}
#objc private func tapped(){
guard let popVC = storyboard?.instantiateViewController(withIdentifier: "popVC") else { return }
popVC.modalPresentationStyle = .popover
let popOverVC = popVC.popoverPresentationController
popOverVC?.delegate = self
popOverVC?.sourceView = self.textSettings
popOverVC?.sourceRect = CGRect(x: self.textSettings.bounds.midX, y: self.textSettings.bounds.maxY, width: 0, height: 0)
popVC.preferredContentSize = CGSize(width: 250, height: 250)
self.present(popVC, animated: true)
}
}
extension SecondViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}
Other controllers have no special code. Video illustrating the problem - https://drive.google.com/file/d/1HUV26H4VFoKglh8FbkNP0y2Y3rYl4Q_f/view?usp=sharing

How to change label text of a label in a popover view controller when pressing a button in the main view?

I'm relatively new to Swift. I have a main view controller, ViewControllerMain, and a popover view controller, PopUpVC, which only has a label. I have a function, infoClicked that displays the popover with another function (showPopover) when the info button is clicked. When I click the button, I want to change the label text of the popover. However, with the current code, the label always displays "Default".
Here is my code:
class ViewControllerMain: UIViewController, UIPopoverPresentationControllerDelegate, GetTimesUsed {
let tipController: PopUpVC = PopUpVC().self
#IBAction func infoClicked(_ sender: UIButton) {
tipController.tipText = "Success"
showPopover()
}
func showPopover() {
let myViewController = storyboard?.instantiateViewController(withIdentifier: "popupController")
myViewController?.preferredContentSize = CGSize(width: 350, height: 200)
myViewController?.modalPresentationStyle = .popover
let popOver = myViewController?.popoverPresentationController
popOver?.delegate = self
UIView.animate(withDuration: 1, animations: {
self.GifView.alpha = 0.7
})
DispatchQueue.main.asyncAfter(deadline: .now() + 0.45) {
UIView.animate(withDuration: 0.5, animations: {
self.present(myViewController!, animated: true, completion: nil)
})
}
popOver?.permittedArrowDirections = .down
popOver?.sourceView = self.view
var passthroughViews: [AnyObject]?
passthroughViews = [infoButton]
myViewController?.popoverPresentationController?.passthroughViews = (NSMutableArray(array: passthroughViews!) as! [UIView])
popOver?.sourceRect = infoButton.frame
}
}
class PopUpVC: UIViewController {
#IBOutlet weak var tip: UILabel!
var tipText: String = "Default"
override func viewDidLoad() {
super.viewDidLoad()
tip.text = tipText
// Do any additional setup after loading the view.
}
}
Thank you for your help.
As mentioned in comments, you seem to be instantiating the popup controller 2 times, so try it like this in your showPopOver code:
let myViewController = storyboard?.instantiateViewController(withIdentifier: "popupController") as! PopUpVC
myViewController.preferredContentSize = CGSize(width: 350, height: 200)
myViewController.modalPresentationStyle = .popover
myViewController.tipText = '' //set to what ever

UIScrollView paging layout on iPad and iPhone

I am making an universal demo xcode project which is a UIScrollView contains two pages, scroll left and right to go back and forth.
My questions is iPad and iPhone's layout is not the same.
I created 3 view controllers in storyboard as below:
ViewController has the UIScrollView.
AViewController contains an UILabel in the center("Good morning...").
BViewController contains an UILabel in the center("Good night...").
The constraints of the UILabels are:
Here is the code of ViewController:
class ViewController: UIViewController {
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let story = UIStoryboard(name: "Main", bundle: nil)
let vc1 = story.instantiateViewController(withIdentifier: "AViewController")
let vc2 = story.instantiateViewController(withIdentifier: "BViewController")
vc1.view.backgroundColor = UIColor.green
vc2.view.backgroundColor = UIColor.red
addContentView([vc1, vc2])
scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width * 2, height: scrollView.frame.height)
}
func addContentView(_ viewControllers: [UIViewController]) {
viewControllers.enumerated().forEach {
addChildViewController($1)
$1.view.frame = CGRect(x: UIScreen.main.bounds.width * CGFloat($0), y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
scrollView.addSubview($1.view)
didMove(toParentViewController: $1)
}
}
}
If I choose View as iPhone 8 then the layout works correct in iPhone 8 like this: (Swipe then from green to red)
But for iPad:
The labels are not centered!!.
If I choose View as iPad ... ,then for iPhone it doesn't layout correct.
Here is the view hierarchy, it's obvious the layout is correct but for some reason the green view is larger than screen size and covered by the red one.
Thanks for any help.
The code is here: https://github.com/williamhqs/TestLayout
The embedded view controller views have translatesAutoresizingMaskIntoConstraints = true by default which introduces bogus constraints:
Setting it to false will not let you just set the frame manually, so you will have to write the full layout. It's slightly more verbose, but autolayout will work for the embedded view controllers and you get rotation support as well:
#IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
let story = UIStoryboard(name: "Main", bundle: nil)
let vc1 = story.instantiateViewController(withIdentifier: "AViewController")
let vc2 = story.instantiateViewController(withIdentifier: "BViewController")
vc1.view.backgroundColor = UIColor.green
vc2.view.backgroundColor = UIColor.red
addContentView([vc1, vc2])
}
func addContentView(_ viewControllers: [UIViewController]) {
var previousController: UIViewController? = nil
viewControllers.forEach {
addChildViewController($0)
$0.view.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview($0.view)
$0.view.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
$0.view.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true
if let trailing = previousController?.view.trailingAnchor {
$0.view.leadingAnchor.constraint(equalTo: trailing).isActive = true
} else {
$0.view.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
}
$0.view.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
$0.view.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
didMove(toParentViewController: self)
previousController = $0
}
previousController?.view.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
}
Scroll view works as expected for all devices, regardless of the Xcode View as setting:

Unrecognized Selector for SideBar Instance

I'm asking this question because the answer I received in this question: How Do I Initialize Two Instances of NSObject in the same ViewController - Swift
brought me in this direction. Whether I subclass as NSObject or UIViewController, I still receive the unrecognized selector when I change my code to what's below.
I'm still trying to be able to create a left and right SideBar. However, I cannot even get one SideBar to load now. I receive an error [UIViewController Center]: unrecognized selector sent to instance xxxxx.
This question is different from the other unrecognized selector instance questions I've seen because I'm not dealing with a button and have no outlets since everything is done programmatically. Hence, I can't specify a subclass that links to a UIViewController in the storyboard.
I feel like once I solve the selector issue the code will work. As the code is right now, the application compiles fine. The problem is the runtime error that I receive. I can provide information from the debugger if it's necessary.
Fwiw, it seems like the runtime is recognizing that I have a center, left, and right viewcontroller from the error message.
I haven't included the RightSideBar code because I think if the left SideBar runs the RightSideBar will run when I add the solution. I want to keep the code you have to read through as brief as possible.
Last thing to note, I'm reaching all of my print statements. I actually print through to the point where it says "I should be showing the sideBar".
Here is the code for the SideBar:
//optional delegate methods that select when the sidebar opens and closes.
#objc protocol SideBarDelegate : class {
func sideBarDidSelectButtonAtIndex (itemIndex: Int)
optional func sideBarWillClose()
optional func sideBarWillOpen()
}
//this class sets up the actual sidebar.
class SideBar: UIViewController, SidebarTableViewControllerDelegate {
//width of the bar, tableview setup, and views for the sidebar
let barWidth:CGFloat = 175.0
let sideBarTableViewTopInset:CGFloat = 25.0
let sideBarContainerView:UIViewController = UIViewController()
let sideBarTableViewController:SidebarTableViewController = SidebarTableViewController()
var originView:UIViewController?
//var for dynamic effect and controlling the sidebar
var animator:UIDynamicAnimator!
weak var delegate:SideBarDelegate?
var isSideBarOpen:Bool = false
//initializer for the "SideBar" class.
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(nibName NibNameOrNil:String!, bundle nibBundleOrNil:NSBundle!) {
super.init(nibName: nil, bundle: nil)
}
convenience init(){
self.init(nibName: nil, bundle: nil)
}
//initializer for the tableView of menu items.
init(sourceView: UIViewController, menuItems: Array<String>, menuImages: [UIImage]){
self.originView = sourceView
self.sideBarTableViewController.tableData = menuItems
self.sideBarTableViewController.imageData = menuImages
println("set initialization values")
super.init(nibName: nil, bundle: nil)
//initializing the views and animation for the menu.
setupSideBar()
animator = UIDynamicAnimator(referenceView: originView!.view)
println("finished initialization")
//swipe gesture recognition for opening the menu.
let showGestureRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
showGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Right
originView!.view.addGestureRecognizer(showGestureRecognizer)
let hideGestureRecognizer:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipe:")
hideGestureRecognizer.direction = UISwipeGestureRecognizerDirection.Left
originView!.view.addGestureRecognizer(hideGestureRecognizer)
}
override func viewDidLoad() {
println("view loaded")
}
//this function handles the direction of swipes
func handleSwipe(recognizer: UISwipeGestureRecognizer){
if recognizer.direction == UISwipeGestureRecognizerDirection.Left {
showSideBar(false)
delegate?.sideBarWillClose?()
println("closed the sideBar")
} else {
println("opened the sideBar")
showSideBar(true)
delegate?.sideBarWillOpen?()
}
}
//function for setting up the sidebar.
func setupSideBar () {
println("setup sideBar")
//setting up the frame/outline of the side bar.
sideBarContainerView.view.frame = CGRectMake(-barWidth - 1, originView!.view.frame.origin.y, barWidth, originView!.view.frame.size.height)
//setting up the color of the sidebar.
sideBarContainerView.view.backgroundColor = UIColor.clearColor()
//disables subviews from being confined to the sidebar.
sideBarContainerView.view.clipsToBounds = false
//placing the sidebar in the UIView
originView!.view.addSubview(sideBarContainerView.view)
//adding blur to the menu.
let blurView:UIVisualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Light))
blurView.frame = sideBarContainerView.view.bounds
sideBarContainerView.view.addSubview(blurView)
//setting up controls for the sidebar
sideBarTableViewController.delegate = self
sideBarTableViewController.tableView.frame = sideBarContainerView.view.bounds
sideBarTableViewController.tableView.clipsToBounds = false
//disabling the scroll feature. Delete to keep the scroll feature.
sideBarTableViewController.tableView.scrollsToTop = false
//This will remove separators in the UITableCell. Delete to keep separators.
sideBarTableViewController.tableView.separatorStyle = UITableViewCellSeparatorStyle.None
//This sets the background color of the sidebar and creates the inset.
sideBarTableViewController.tableView.backgroundColor = UIColor.clearColor()
sideBarTableViewController.tableView.contentInset = UIEdgeInsets(top: sideBarTableViewTopInset, left: 0, bottom: 0, right: 0)
//reloads the sidebar and adds the container view to the sideBarTableViewController.
sideBarTableViewController.tableView.reloadData()
sideBarContainerView.view.addSubview(sideBarTableViewController.tableView)
originView?.addChildViewController(sideBarContainerView)
sideBarContainerView.didMoveToParentViewController(originView)
}
func showSideBar(shouldOpen: Bool){
animator.removeAllBehaviors()
isSideBarOpen = shouldOpen
println("I should be showing the sideBar")
//simple if and else statements to define the direction of animation and intensity of animation
let gravityX:CGFloat = (shouldOpen) ? 0.5 : -0.5
let magnitude:CGFloat = (shouldOpen) ? 20 : -20
let boundaryX:CGFloat = (shouldOpen) ? barWidth : -barWidth - 1
//controls the behavior of the animation.
let gravityBehavior: UIGravityBehavior = UIGravityBehavior(items: [sideBarContainerView])
gravityBehavior.gravityDirection = CGVectorMake(gravityX, 0)
animator.addBehavior(gravityBehavior)
let collisionBehavior: UICollisionBehavior = UICollisionBehavior(items: [sideBarContainerView])
collisionBehavior.addBoundaryWithIdentifier("sideBarBoundary", fromPoint: CGPointMake(boundaryX, 20), toPoint: CGPointMake(boundaryX, originView!.view.frame.size.height))
animator.addBehavior(collisionBehavior)
let pushBehavior:UIPushBehavior = UIPushBehavior(items: [sideBarContainerView], mode: UIPushBehaviorMode.Instantaneous)
pushBehavior.magnitude = magnitude
animator.addBehavior(pushBehavior)
let sideBarBehavior:UIDynamicItemBehavior = UIDynamicItemBehavior(items: [sideBarContainerView])
sideBarBehavior.elasticity = 0.3
animator.addBehavior(sideBarBehavior)
}
func sidebarControlDidSelectRow(indexPath: NSIndexPath) {
delegate?.sideBarDidSelectButtonAtIndex(indexPath.row)
}
}
Here is the Home ViewController:
class Home: UIViewController, SideBarDelegate {
//*** Must put logout code into the logout button it should log the user out if they press it ***
var sideBar:SideBar = SideBar()
var homeImage = UIImage(named: "Shine Home")
var profileImage = UIImage(named: "Shine Profile")
var shareImage = UIImage(named: "Shine Share")
var aboutImage = UIImage(named: "Shine About")
var helpImage = UIImage(named: "Shine Help")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//setting up the menu items for the sidebar.
sideBar = SideBar(sourceView: self, menuItems: ["Home", "Profile", "Share", "About", "Help"], menuImages: [homeImage!, profileImage!, shareImage!, aboutImage!, helpImage!])
sideBar.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func sideBarDidSelectButtonAtIndex(itemIndex: Int) {
switch itemIndex {
case 0:
let vc = storyboard?.instantiateViewControllerWithIdentifier("Home") as! Home
self.navigationController?.pushViewController(vc, animated: true)
case 1:
performSegueWithIdentifier("profile", sender: self)
case 2:
performSegueWithIdentifier("share", sender: self)
case 3:
performSegueWithIdentifier("about", sender: self)
case 4:
performSegueWithIdentifier("help", sender: self)
default:
break
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
This situation of unrecognized selector stems from the runtime not knowing what specific object you want to activate.
The runtime sees sideBarContainerView object, which is acceptable for the compiler.
The problem is that your animation behavior is for views. Your UIViewController Object has not descended into UIViews. You need to add .view to all sideBarContainerView in your showSideBar function.
If you breakdown your function and just use isSideBarOpen as the bool for your if statements in the constants and place it in the initialization of your menu then you'll get the Selector error immediately.
That's just a second way to point to fact that the error is past your last print statement "I should be showing SideBar.
If you do that then your menu will load. So far, I have no solution for getting both menus to initialize as a left and right menu.

Get the frame of UIBarButtonItem in Swift?

how could I get the frame of a rightbarbuttonItem in swift? I found this : UIBarButtonItem: How can I find its frame? but it says cannot convert NSString to UIView, or cannot convert NSString to String :
let str : NSString = "view" //or type String
let theView : UIView = self.navigationItem.rightBarButtonItem!.valueForKey("view")//or 'str'
The goal is to remove the rightBarButtonItem, add an imageView instead, and move it with a fadeOut effect.
Thanks
You should try it like:
var barButtonItem = self.navigationItem.rightBarButtonItem!
var buttonItemView = barButtonItem.valueForKey("view")
var buttonItemSize = buttonItemView?.size
Edit (Swift 3):
var barButtonItem = self.navigationItem.rightBarButtonItem!
let buttonItemView = barButtonItem.value(forKey: "view") as? UIView
var buttonItemSize = buttonItemView?.size
In Swift4, XCode9
for view in (self.navigationController?.navigationBar.subviews)! {
if let findClass = NSClassFromString("_UINavigationBarContentView") {
if view.isKind(of: findClass) {
if let barView = self.navigationItem.rightBarButtonItem?.value(forKey: "view") as? UIView {
let barFrame = barView.frame
let rect = barView.convert(barFrame, to: view)
}
}
}
}
import UIKit
class ViewController: UIViewController {
let customButton = UIButton(type: .system)
override func viewDidLoad() {
super.viewDidLoad()
customButton.setImage(UIImage(named: "myImage"), for: .normal)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: customButton)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print(self.customButton.convert(self.customButton.frame, to: nil))
}
}
You must use UIBarButtonItem objects created with customView for this; UIBarButtonItem objects created the regular way don't have enough information exposed. It's important that the frame be looked up after the parent UINavigationController's UINavigationBar has completely laid out its entire subview tree. For most use cases, the visible UIViewController's viewDidAppear is a good enough approximation of this.

Resources