UITextView text disappears after applying transform - ios

I have a simple UITextView with a custom NSLayoutManager in my app. The UITextView have a tap gesture and when you tap on UITextView it animates off the screen. My problem is that when you tap on the UITextView the entire text disappears. After debugging for a while I think it has something to do with NSTextContainer which I pass to the UITextView because If I don't pass it everything works fine. I thought it had to do something with my custom NSLayoutManager but then I tested it with a default NSLayoutManger and it is still giving the same issue.
Can you please tell me what is going wrong? I have attached a sample code for your reference. I have slowed down the animation so can see what is wrong.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setUpTextView()
}
private func setUpTextView() {
let rect = CGRect(origin: CGPoint(x: view.frame.origin.x,
y: view.frame.origin.y + 50
),
size: CGSize(width: view.frame.width,
height: view.frame.height - 50
)
)
let textView = MyTextView(frame: rect)
textView.backgroundColor = .gray
textView.layer.cornerRadius = 15
textView.textContainerInset.left = view.frame.width * 0.05
textView.textContainerInset.right = view.frame.width * 0.05
textView.textContainerInset.top = view.frame.height * 0.05
textView.textContainerInset.bottom = view.frame.height * 0.05
let string = "The Ultimate Fighting Championship is an American mixed martial arts promotion company based in Las Vegas, Nevada."
let attributedText = NSAttributedString(string: string, attributes: [
.font: UIFont.systemFont(ofSize: 24, weight: .semibold),
.foregroundColor: UIColor.white
])
textView.attributedText = attributedText
textView.isScrollEnabled = false
textView.isEditable = false
textView.isSelectable = false
view.addSubview(textView)
}
}
MyTextView
class MyTextView: UITextView {
override init(frame: CGRect, textContainer: NSTextContainer?) {
let textStorage = NSTextStorage()
let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
let textContainer = NSTextContainer(size: frame.size)
layoutManager.addTextContainer(textContainer)
super.init(frame: frame, textContainer: textContainer)
addGesture()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func addGesture() {
let gesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
addGestureRecognizer(gesture)
}
#objc func handleTap(gesture: UITapGestureRecognizer) {
UIView.animate(withDuration: 10, delay: 0) {
self.transform = CGAffineTransform(translationX: 0, y: self.frame.height)
}
}
}

The problem you're running into is that you are positioning your view "off-screen."
When UIKit generates the animation, it calculates the final position and decides that, because your text view will no longer be visible, it doesn't need to render the text container content.
If you want to continue using transform (although, I'd advise against that and instead use auto-layout), you can fix your disappearing text by animating the textView so 1-point is still inside the view's bounds, and then moving it the final point in the completion block.
Something like this:
UIView.animate(withDuration: 10.0, animations: {
self.transform = CGAffineTransform(translationX: 0, y: self.frame.height - 1.0)
}, completion: { _ in
self.transform = CGAffineTransform(translationX: 0, y: self.frame.height)
})

Related

Custom UIView class is not animating as expected

I have a custom UIView class that creates a square with a red background and I'm trying to get a green square slide on top of it from the left. I have a button from the main view that brings me to the screen above but the green screen doesn't animate over it. It just appears at the end result. I do know that I am creating these shapes when I change the initial width of the green square but no indication of an animation.
MainViewController.swift
private func configureAnimatedButton() {
//let sampleAnimatedButton
let sampleAnimatedButton = AnimatedButtonView()
sampleAnimatedButton.center = CGPoint(x: self.view.frame.size.width / 2,
y: self.view.frame.size.height / 2)
self.view.addSubview(sampleAnimatedButton)
sampleAnimatedButton.animateMiddleLayer()
}
AnimatedButton.swift
import Foundation
import UIKit
public class AnimatedButtonView: UIView {
//initWithFrame to init view from code
let movingLayer = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
//initWithCode to init view from xib or storyboard
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupView()
}
//common func to init our view
private func setupView() {
//backgroundColor = .red
self.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
self.backgroundColor = .red
movingLayer.frame = CGRect(x: self.frame.maxX, y: self.frame.minY, width: 0, height: 300)
movingLayer.backgroundColor = .green
self.addSubview(movingLayer)
}
public func animateMiddleLayer() {
UIView.animate(withDuration: 10.0, delay: 1.2, options: .curveEaseOut, animations: {
self.layoutIfNeeded()
var grayLayerTopFrame = self.movingLayer.frame
grayLayerTopFrame.origin.x = 0
grayLayerTopFrame.size.width += 300
self.movingLayer.frame = grayLayerTopFrame
self.layoutIfNeeded()
}, completion: { finished in
print("animated")
})
}
}
You should move grayLayerTopFrame outside the UIView.animate(), before it starts. Also, you don't need layoutIfNeeded if you're just animating the frame (you use it when you have constraints).
var grayLayerTopFrame = self.movingLayer.frame
grayLayerTopFrame.origin.x = 0
grayLayerTopFrame.size.width += 300
UIView.animate(withDuration: 10.0, delay: 1.2, options: .curveEaseOut, animations: {
self.layoutIfNeeded()
self.movingLayer.frame = grayLayerTopFrame
self.layoutIfNeeded()
}, completion: { finished in
print("animated")
})
Also, make sure you don't call it in viewDidLoad() -- at that time, the views aren't laid out yet.

Creating a haptic-touch-like button in Swift/Obj-C

I've been playing around with an idea for a button which, when held, expands to reveal other buttons (like the FAB in Android). Without releasing, sliding one's finger down should highlight other buttons, similar to haptic touch's behaviour.
Here's a crude mockup I've created using Drama: https://youtu.be/Iam8Gjv3gqM.
There should also be an option to have a label horizontally next to each button, and the order of buttons should be as shown (with the selected button at the top instead of its usual position).
I already have a class for each button (seen below), but do not know how to achieve this layout/behaviour.
class iDUButton: UIButton {
init(image: UIImage? = nil) {
super.init(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
if let image = image {
setImage(image, for: .normal)
}
backgroundColor = .secondarySystemFill
layer.cornerRadius = 15
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}
class iDUBadgeButton: iDUButton {
var badgeLabel = UILabel()
var badgeCount = 0
override init(image: UIImage? = nil) {
super.init(image: image)
setup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setup()
}
private func setup() {
badgeLabel.textColor = .white
badgeLabel.backgroundColor = .systemRed
badgeLabel.textAlignment = .center
badgeLabel.font = .preferredFont(forTextStyle: .caption1)
badgeLabel.alpha = 0
updateBadge()
addSubview(badgeLabel)
}
override func layoutSubviews() {
super.layoutSubviews()
badgeLabel.sizeToFit()
let height = max(18, badgeLabel.frame.height + 5.0)
let width = max(height, badgeLabel.frame.width + 10.0)
badgeLabel.frame = CGRect(x: frame.width - 5, y: -badgeLabel.frame.height / 2, width: width, height: height);
badgeLabel.layer.cornerRadius = badgeLabel.frame.height / 2;
badgeLabel.layer.masksToBounds = true;
}
func setBadgeCount(badgeCount: Int) {
self.badgeCount = badgeCount;
self.updateBadge()
}
func updateBadge() {
if badgeCount != 0 {
badgeLabel.text = "\(badgeCount)"
}
UIView.animate(withDuration: 0.25, animations: {
self.badgeLabel.alpha = self.badgeCount == 0 ? 0 : 1
})
layoutSubviews()
}
}
If anyone could help with achieving this layout, I'd be very grateful! Thanks in advance.
PS: I'm developing the UI in Swift, but will be converting it to Objective-C once complete. If you're interested, the use case is to adapt the button in the top-left of this to offer a selection of different tags.

Why is my UIVisualEffectView show in viewHierarchy but not in my device?

I am facing an incomprehensible problem.
I have a login UIViewController and a ProgressLoading UIVisualEffectView.
I want to print the loading when I am making an API call and waiting for response.
Here is myProgressLoading Class
import UIKit
class ProgressLoading: UIVisualEffectView {
var text: String? {
didSet {
label.text = text
}
}
let activityIndictor: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
let label: UILabel = UILabel()
let blurEffect = UIBlurEffect(style: .light)
let vibrancyView: UIVisualEffectView
init(text: String) {
self.text = text
self.vibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: blurEffect))
self.vibrancyView.backgroundColor = UIColor(white: 0.2, alpha: 0.7)
super.init(effect: blurEffect)
self.setup()
}
required init?(coder aDecoder: NSCoder) {
self.text = ""
self.vibrancyView = UIVisualEffectView(effect: UIVibrancyEffect(blurEffect: blurEffect))
self.vibrancyView.backgroundColor = UIColor(white: 0.2, alpha: 0.7)
super.init(coder: aDecoder)
self.setup()
}
func setup() {
contentView.addSubview(vibrancyView)
contentView.addSubview(activityIndictor)
contentView.addSubview(label)
activityIndictor.startAnimating()
}
override func didMoveToSuperview() {
super.didMoveToSuperview()
if let superview = self.superview {
let width = superview.frame.size.width / 2.3
let height: CGFloat = 50.0
self.frame = CGRect(x: superview.frame.size.width / 2 - width / 2,
y: superview.frame.height / 2 - height / 2,
width: width,
height: height)
vibrancyView.frame = self.bounds
let activityIndicatorSize: CGFloat = 40
activityIndictor.frame = CGRect(x: 5,
y: height / 2 - activityIndicatorSize / 2,
width: activityIndicatorSize,
height: activityIndicatorSize)
activityIndictor.color = UIColor.white
layer.cornerRadius = 8.0
layer.masksToBounds = true
label.text = text
label.textAlignment = NSTextAlignment.center
label.frame = CGRect(x: activityIndicatorSize + 5,
y: 0,
width: width - activityIndicatorSize - 15,
height: height)
label.textColor = UIColor.white
label.font = UIFont.boldSystemFont(ofSize: 16)
}
}
func show() {
self.isHidden = false
}
func hide() {
self.isHidden = true
}
}
Here is how I work with my progressLoading, a show and hide methods and declare with a text.
override func viewDidLoad() {
super.viewDidLoad()
progressLoading = ProgressLoading(text: "Loggin in...")
progressLoading?.hide()
self.view.addSubview(progressLoading!)
}
func startAnimatingLoading(viewModel: Login.ViewModel) {
self.progressLoading?.show()
}
func stopAnimatingLoading(viewModel: Login.ViewModel) {
self.progressLoading?.hide()
}
My problem is that when I wait for the response, I show the loading programatically, but nothing appears in my device. (if you wonder, I simulate long API callback by just making a breakpoint and stay on the breakpoint)
I looked inside the viewHierarchy and the Loading is right here in front of everything exactly how I want it to be.
Here is my ViewHierarchy :
Is there something I didn't get with the viewHierarchy ?
How is this possible that something is shown on the view hierarchy but not inside my device ?
Thanks you for your help, I just don't get it !
Are you making the API call on the main thread? If you start the animation and then block the main thread the progress indicator won't appear - the system needs at least one draw cycle to actually display the indicator. Move your simulated API call to a background thread and then show your indicator before the call and hide it when API responds. Hope this helps!

Zoom on UIView contained in UIScrollView

I have some trouble handling zoom on UIScrollView that contains many subviews. I already saw many answers on SO, but none of them helped me.
So, basically what I'm doing is simple : I have a class called UIFreeView :
import UIKit
class UIFreeView: UIView, UIScrollViewDelegate {
var mainUIView: UIView!
var scrollView: UIScrollView!
var arrayOfView: [UIView]?
override init(frame: CGRect) {
super.init(frame: frame)
self.setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
fileprivate func setupView() {
// MainUiView
self.mainUIView = UIView(frame: self.frame)
self.addSubview(mainUIView)
// ScrollView
self.scrollView = UIScrollView(frame: self.frame)
self.scrollView.delegate = self
self.addSubview(self.scrollView)
}
func reloadViews(postArray:[Post]?) {
if let postArray = postArray {
print("UIFreeView::reloadVIew.postArraySize = \(postArray.count)")
let size: CGFloat = 80.0
let margin: CGFloat = 20.0
scrollView.contentSize.width = (size * CGFloat(postArray.count))+(margin*CGFloat(postArray.count))
scrollView.contentSize.height = (size * CGFloat(postArray.count))+(margin*CGFloat(postArray.count))
for item in postArray {
let view = buildPostView(item)
self.scrollView.addSubview(view)
}
}
}
fileprivate func buildPostView(_ item:Post) -> UIView {
// Const
let size: CGFloat = 80.0
let margin: CGFloat = 5.0
// Var
let view = UIView()
let textView = UITextView()
let backgroundImageView = UIImageView()
// Setup view
let x = CGFloat(UInt64.random(lower: UInt64(0), upper: UInt64(self.scrollView.contentSize.width)))
let y = CGFloat(UInt64.random(lower: UInt64(0), upper: UInt64(self.scrollView.contentSize.height)))
view.frame = CGRect(x: x,
y: y,
width: size,
height: size)
// Setup background view
backgroundImageView.frame = CGRect(x: 0,
y: 0,
width: view.frame.size.width,
height: view.frame.size.height)
var bgName = ""
if (item.isFromCurrentUser) {
bgName = "post-it-orange"
} else {
bgName = "post-it-white"
}
backgroundImageView.contentMode = .scaleAspectFit
backgroundImageView.image = UIImage(named: bgName)
view.addSubview(backgroundImageView)
// Setup text view
textView.frame = CGRect(x: margin,
y: margin,
width: view.frame.size.width - margin*2,
height: view.frame.size.height - margin*2)
textView.backgroundColor = UIColor.clear
textView.text = item.content
textView.isEditable = false
textView.isSelectable = false
textView.isUserInteractionEnabled = false
view.addSubview(textView)
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
view.addGestureRecognizer(gestureRecognizer)
return view
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.scrollView
}
func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.scrollView)
// note: 'view' is optional and need to be unwrapped
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
gestureRecognizer.setTranslation(CGPoint.zero, in: self.scrollView)
}
}
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
}
This class is working perfectly, I can scroll through all my views, but I can't zoom-in and zoom-out in order to see less/more views on my screen.
I think the solution is simple, but I can't seem to find a real solution for my problem ..
Thanks !

UITextfield gets cut from the top while in editing mode

I got two UITextfields with separator(UILabel) between them.
I put them all into UIStackView.
While in editing mode, content of the textfield is cut from the top, as seen in the picture below
I've found that the only way to remove this issue is to make this separator big enough, but this spoils my design.
How to fix it?
It's worth to mention my UIStackView settings:
and show how I implement this custom bottomline-style UITextfield
class CustomTextField: UITextField {
override func awakeFromNib() {
super.awakeFromNib()
let attributedString = NSAttributedString(string: self.placeholder!, attributes: [NSForegroundColorAttributeName:UIColor.lightGray, NSFontAttributeName: UIFont(name: "GothamRounded-Book", size: 18.0)! ])
self.attributedPlaceholder = attributedString
self.tintColor = UIColor.appRed
self.font = UIFont(name: "GothamRounded-Book", size: 18.0)!
self.borderStyle = .none
self.textAlignment = .center
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 0, dy: 5)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: 0, dy: 5)
}
override var tintColor: UIColor! {
didSet {
setNeedsDisplay()
}
}
override func draw(_ rect: CGRect) {
let startingPoint = CGPoint(x: rect.minX, y: rect.maxY)
let endingPoint = CGPoint(x: rect.maxX, y: rect.maxY)
let path = UIBezierPath()
path.move(to: startingPoint)
path.addLine(to: endingPoint)
path.lineWidth = 2.0
tintColor.setStroke()
tintColor = UIColor.appRed
path.stroke()
}
}
Any help much appreciated
EDIT
I have another TextField like that and it works fine, but it doesn't sit inside any horizontal UIStackView. Here is the screenshot of hierarchy:
Unfortunately you need to check the size on editing
class CustomTextField: UITextField {
override func awakeFromNib() {
super.awakeFromNib()
self.addTarget(self, action: #selector(textFieldEditingChanged), for: .editingChanged)
}
func textFieldEditingChanged(_ textField: UITextField) {
textField.invalidateIntrinsicContentSize()
}
override var intrinsicContentSize: CGSize {
if isEditing {
let string = text ?? ""
let size = string.size(attributes: typingAttributes)
return CGSize(width: size.width + (rightView?.bounds.size.width ?? 0) + (leftView?.bounds.size.width ?? 0) + 2,
height: size.height)
}
return super.intrinsicContentSize
}
}

Resources