How do I refresh when touch drag is finished on Swift? - ios

I have a problem with the refresh part. Currently, the function to refresh is OK.
But I want to refresh when the user has finished touching the iphone.
var refController:UIRefreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
refController.bounds = CGRect.init(x: 0.0, y: 40.0, width: refController.bounds.size.width, height: refController.bounds.size.height)
refController.addTarget(self, action: #selector(self.webviewRefresh(refresh:)), for: .valueChanged)
refController.attributedTitle = NSAttributedString(string: "refreshing")
WKWebView.scrollView.addSubview(refController)
if contentController.userScripts.count > 0 {
contentController.removeAllUserScripts()
}
...
}
#objc func webviewRefresh(refresh:UIRefreshControl){
refController.endRefreshing()
WKWebView.reload()
}
Currently, Even if user haven't finished touch at this time, when the screen is lowered to a certain height, a refresh is performed on the screen.
But I want to run a refresh when the user has finished touching the screen.

You have to call refresh method in scrollViewDidEndDecelerating delegate method of UIScrollViewDelegate. So import UIScrollViewDelegate in your ViewController Like,
extension YourViewController: UIScrollViewDelegate {
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if self.refController.isRefreshing {
self.webviewRefresh()
}
}
}
Then your webviewRefresh will be,
#objc func webviewRefresh(){
refController.endRefreshing()
WKWebView.reload()
}
And yourviewDidLoad will be,
override func viewDidLoad() {
super.viewDidLoad()
refController.bounds = CGRect.init(x: 0.0, y: 40.0, width: refController.bounds.size.width, height: refController.bounds.size.height)
refController.attributedTitle = NSAttributedString(string: "refreshing")
WKWebView.scrollView.addSubview(refController)
WKWebView.scrollView.delegate = self
if contentController.userScripts.count > 0 {
contentController.removeAllUserScripts()
}
}

You can use crrefresh library for doing this thing

Related

UIScrollView zoomScale not updating after initial update

I have a tap gesture that runs this code and it works once but then stops updating the zoomScale.
#objc func sampleTapGestureTapped(_ recognizer: UITapGestureRecognizer) {
print("tapped")
if self.scrollView_Image.zoomScale > self.scrollView_Image.minimumZoomScale {
scrollView_Image.setZoomScale(1, animated: false)
} else {
scrollView_Image.setZoomScale(3, animated: false)
}
}
The function runs and the tapped print is logged out but the zoomScale doesn't seem to change.
Perhaps the problem is your hard coded numbers. Here is how I do it:
if sv.zoomScale < sv.maximumZoomScale {
sv.setZoomScale(sv.maximumZoomScale, animated:anim)
}
else {
sv.setZoomScale(sv.minimumZoomScale, animated:anim)
}
Notice there are no hard coded numbers here. It works for any scroll view.
I've tried this in a small View Controller and it is working fine. It is possible that an action you are taking in the selector is stopping the gesture from working. You should probably post the selector function code as well, and anything relevant to setting up the Recognizer and the Image View
class ViewController: UIViewController {
var tappableImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
tappableImageView = UIImageView(frame: CGRect(x: 40, y: 40, width: 100, height: 100))
tappableImageView.backgroundColor = .red
view.addSubview(tappableImageView)
tappableImageView.isUserInteractionEnabled = true
let t = UITapGestureRecognizer(target: self, action: #selector(imageViewDoubleTapped(_:)))
t.numberOfTapsRequired = 2
tappableImageView.addGestureRecognizer(t)
}
#objc func imageViewDoubleTapped(_ recognizer: UITapGestureRecognizer) {
print("Double Tapped")
}
}

Adding a menu overlay to a UICollectionView using LongPress gesture

I've got a UICollectionView that typically contains 100-200 cells that scrolls both horizontally and vertically. I am trying to provide a simple UIView popup with 3 icons when the user performs a LongPress gesture to allow them to easily navigate to a couple specific cells within the UICollectionView.
I'd like the start of the LongPress to bring up the popup, then the user would drag their finger to one of the three icons and release to select that icon.
The problem I'm having is I can't figure out how to get either the view containing the 3 icons or the icons themselves to respond to the end of the LongPress gesture.
Here's a simplification of the UIViewController:
class MyViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
#IBOutlet weak var myCollectionView: UICollectionView!
#IBAction func longTouch(_ sender: UILongPressGestureRecognizer) {
if sender.state == .began {
self.menu!.frame = CGRect(x: sender.location(in: self.view).x - 20,
y: sender.location(in: self.view).y - 75,
width: 100, height: 100)
self.menu!.isHidden = false
}
else if sender.state == .ended {
self.menu!.isHidden = true
}
}
// Popup for long touch
var menu: UIView?
// MARK: UIViewController
override func viewDidLoad() {
super.viewDidLoad()
self.initMenu()
}
// MARK:- UICollectionViewDelegate
...
// MARK:- UICollectionViewDataSource
...
// MARK:- UIScrollViewDelegate
...
private func initMenu() -> Void {
self.menu = UIView()
self.menu!.isUserInteractionEnabled = true
self.menu!.frame = CGRect(x: 0,
y: 0,
width: 100,
height: 50)
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(menuLongPressHandler))
self.menu!.addGestureRecognizer(longPress)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
self.menu!.addGestureRecognizer(tapGesture)
// There would be ImageView icons here as well, which I've left out for simplicity
self.menu!.isHidden = true
}
func menuLongPressHandler() {
print("Long Press")
}
func handleTap(sender: UITapGestureRecognizer) {
print("Tap")
}
}
Neither the "Tap" or "Long Press" are printed when the LongPress ends while over the View. Any suggestions on how to get the view to capture the end of that LongPress gesture?

becomeFirstResponder not showing from viewDidLoad

Here I have a simple example. im calling FirstResponder in viewDidLoad. But accessory view only shows up after tapping the screen. Why isn't it showing from the start?
class TestViewController: MainPageViewController {
private let accessoryView = UIView() //TextInputView() // MessageInputAccessoryView()
override var inputAccessoryView: UIView {
return accessoryView
}
override var canBecomeFirstResponder: Bool { return true }
override func viewDidLoad() {
super.viewDidLoad()
accessoryView.backgroundColor = .red
accessoryView.frame = CGRect(x: 0, y: 0, width: 300, height: 50)
self.becomeFirstResponder()
// Do any additional setup after loading the view.
let tap = UITapGestureRecognizer(target: self, action: #selector(tappo))
self.view.isUserInteractionEnabled = true
self.view.addGestureRecognizer(tap)
tappo()
}
func tappo() {
self.becomeFirstResponder()
}
}
viewWillAppear is a better place to put the becomesFirstResponder. Try that.
So something was resigning my first responder (as I was using UIPageViewController). So I've added this to my UIViewControler :
override var canResignFirstResponder: Bool { return false }
That's it. Cheers!

Calculate button height of keyboard buttons

I'm working on a universal keyboard extension. Currently I want to calculate the height of a single button. The keyboard has 4 rows as the normal iOS keyboard. Because the space between the buttons (y distance) is 5 I calculated: (self.view.frame.height-(5*5))/4 5x5 because the distance to the top and the bottom is included. Now, if I run the app, one button isn't around 1/4 of the screen. Instead the button's height is around 2/3 of the keyboard view. I really can't find my mistake. I hope you can help me with this calculation.
My current code:
import UIKit
class KeyboardViewController: UIInputViewController, UIScrollViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
print((self.view.frame.height-25)/4)
addButtons()
}
func addButtons() {
let buttonHeigth = (self.view.frame.height-25)/4
let button = ResizableButton()
button.frame = CGRect(x: CGFloat(5), y: CGFloat(10), width: CGFloat(button.frame.size.width), height: CGFloat(buttonHeigth))
button.addTarget(self, action: #selector(keyPressed(sender:)), for: .touchUpInside)
button.layer.cornerRadius = 10
button.layer.masksToBounds = false
self.scrollView.addSubview(button)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated
}
override func textWillChange(_ textInput: UITextInput?) {
// The app is about to change the document's contents. Perform any preparation here.
}
override func textDidChange(_ textInput: UITextInput?) {
// The app has just changed the document's contents, the document context has been updated.
var textColor: UIColor
let proxy = self.textDocumentProxy
if proxy.keyboardAppearance == UIKeyboardAppearance.dark {
textColor = UIColor.white
} else {
textColor = UIColor.black
}
self.nextKeyboardButton.setTitleColor(textColor, for: [])
}
}

Tap Gesture Recognizer not received in custom UIView embedded in super view

I am trying to create a custom UIView/Scrollview named MyScrollView that contains a few labels (UILabel), and these labels receive tap gestures/events in order to respond to user's selections .
In order to make the tap event work on the UILabels, I make sure they all have userIteractionEnabled = true and I created a delegate as below:
protocol MyScrollViewDelegate {
func labelClicked(recognizer: UITapGestureRecognizer)
}
The custom UIView is being used in ScrollViewController that I created, this ScrollViewController implements the delegate method as well:
import UIKit
import Neon
class ScrollViewController: UIViewController, MyScrollViewDelegate {
var curQuestion: IPQuestion?
var type: QuestionViewType?
var lastClickedLabelTag: Int = 0 //
init(type: QuestionViewType, question: IPQuestion) {
super.init(nibName: nil, bundle: nil)
self.curQuestion = question
self.type = type
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func loadView() {
view = MyScrollView(delegate: self, q: curQuestion!)
view.userInteractionEnabled = true
}
}
// implementations for MyScrollViewDelegate
extension ScrollViewController {
func labelTitleArray() -> [String]? {
print("labelTitleArray called in implemented delegate")
return ["Comments", "Answers"]
}
func labelClicked(recognizer: UITapGestureRecognizer) {
print("labelClicked called in implemented delegate")
let controller = parentViewController as? ParentViewController
controller?.labelClicked(recognizer)
lastClickedLabelTag = recognizer.view!.tag
}
}
// MARK: - handle parent's ViewController event
extension QuestionDetailViewController {
func updateActiveLabelsColor(index: Int) {
print("updating active labels color: \(index)")
if let view = view as? MyScrollView {
for label in (view.titleScroll.subviews[0].subviews as? [UILabel])! {
if label.tag == index {
label.transform = CGAffineTransformMakeScale(1.1,1.1)
label.textColor = UIColor.purpleColor()
}
else {
label.transform = CGAffineTransformMakeScale(1,1)
label.textColor = UIColor.blackColor()
}
}
}
}
}
This above ScrollViewController is added, as a child view controller to the parent view controller, and positioned to the top part of the parent's view:
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
self.view.backgroundColor = UIColor.whiteColor()
addChildViewController(scrollViewController) // added as a child view controller here
view.addSubview(scrollViewController.view) // here .view is MyScrollView
scrollViewController.view.userInteractionEnabled = true
scrollViewController.view.anchorToEdge(.Top, padding: 0, width: view.frame.size.width, height: 100)
}
The app can load everything up in the view, but the tap gesture/events are not passed down to the labels in the custom MyScrollView. For this, I did some google search and have read Event Delivery: Responder Chain on Apple Developer website and did a hit test as well. The hitTest function below can be triggered in the MyScrollView:
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
print("hit test started, point: \(point), event: \(event)")
return self
}
My observations with the hitTest is that the touchesBegan() and touchesEnded() methods are triggered in the view only when the hitTest function is there. Without hitTest, both functions do not get called with taps.
but no luck getting the UILabel to respond to Tap Gestures. So I am reaching out to experts on SO here. Thanks for helping!
I think I found out the reason why the UILabel did not respond to tapping after much struggle: the .addGestureRecognizer() method to the label was run in the init() method of my custom UIView component, which is wrong, because the view/label may not have been rendered yet. Instead, I moved that code to the lifecycle method layoutSubviews(), and everything started to work well:
var lastLabel: UILabel? = nil
for i in 0..<scrollTitleArr.count {
let label = UILabel()
label.text = scrollTitleArr[i] ?? "nothing"
print("label: \(label.text)")
label.font = UIFont(name: "System", size: 15)
label.textColor = (i == 0) ? MaterialColor.grey.lighten2 : MaterialColor.grey.darken2
label.transform = (i == 0) ? CGAffineTransformMakeScale(1.1, 1.1) : CGAffineTransformMakeScale(0.9, 0.9)
label.sizeToFit()
label.tag = i // for tracking the label by tag number
label.userInteractionEnabled = true
label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.labelClicked(_:))))
titleContainer.addSubview(label)
if lastLabel == nil {
label.anchorInCorner(.TopLeft, xPad: 0, yPad: 0, width: 85, height: 40)
// label.anchorToEdge(.Left, padding: 2, width: 85, height: 40)
} else {
label.align(.ToTheRightMatchingTop, relativeTo: lastLabel!, padding: labelHorizontalGap, width: 85, height: 40)
}
lastLabel = label
}
In addition, I don't need to implement any of the UIGestureRecognizer delegate methods and I don't need to make the container view or the scroll view userInteractionEnabled. More importantly, when embedding the custom UIView to a superview, I configured its size and set clipsToBounds = true.
I guess I should have read more UIView documentation on the Apple Developer website. Hope this will help someone like me in the future! Thanks to all!
You have to set the property userInteractionEnabled = YES.
For some reason, my simulator was frozen or something when the tap gesture recognizer wasn't working. So, when I restarted the app, then it all worked again. I don't know if this applies here, but that was the fix for me.

Resources