I want to animate badge count over tabbar something like Bouncing Animation . Has anyone implemented it with native UITabBarController . i am not using any third party class for adding UITabBarController in my project.
i do some thing like this before i will share the code with you
first i create two functions
first one is :
func loopThrowViews(view:UIView){
for subview in (view.subviews){
let type = String(describing: type(of: subview))
print(type)
if type == "_UIBadgeView" {
print("this is BadgeView")
animateView(view: subview)
}
else {
loopThrowViews(view:subview)
}
}
}
this function take view and loop throw all its subViews until it find the badge View then it's call the animate method this one
func animateView(view:UIView){
let shakeAnimation = CABasicAnimation(keyPath: "position")
shakeAnimation.duration = 0.05
shakeAnimation.repeatCount = 50
shakeAnimation.autoreverses = true
shakeAnimation.fromValue = NSValue(cgPoint: CGPoint(x:view.center.x - 10, y:view.center.y))
shakeAnimation.toValue = NSValue(cgPoint: CGPoint(x:view.center.x + 10, y:view.center.y))
view.layer.add(shakeAnimation, forKey: "position")
}
You can replace the code at this method with your own animation
all what you need is to call this method like this when ever you want to animate the badge
loopThrowViews(view: self.tabBarController!.tabBar)
the result will be like this
full example here https://github.com/AliAdam/AnimateTabbarBadgeView
Related
I have some UIButtons that I'm animating indefinitely. The buttons all have 3 sublayers that are added, each of which have their own animation. I'm initializing these animations on viewDidAppear which works great - they fade in and start rotating. The problem is that when I transition to a new view, the animations seem to "snap" back to their initial state, then back to some other state right before the transition occurs. I've tried explicitly removing all of the animations on viewWillDisappear, even tried hiding the entire UIButton itself, but nothing seems to prevent this weird snapping behavior from occurring.
Here's a gif of what's happening (this is me transitioning back and forth between two views):
func animateRotation() {
// Gets called on viewDidAppear
let rotationRight: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotationRight.toValue = Double.pi * 2
rotationRight.duration = 4
rotationRight.isCumulative = true
rotationRight.repeatCount = Float.greatestFiniteMagnitude
rotationRight.isRemovedOnCompletion = false
rotationRight.fillMode = .forwards
let rotationLeft: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotationLeft.toValue = Double.pi * -2
rotationLeft.duration = 3
rotationLeft.isCumulative = true
rotationLeft.repeatCount = Float.greatestFiniteMagnitude
rotationLeft.isRemovedOnCompletion = false
rotationLeft.fillMode = .forwards
let circleImage1 = UIImage(named: "circle_1")?.cgImage
circleLayer1.frame = self.bounds
circleLayer1.contents = circleImage1
circleLayer1.add(rotationRight, forKey: "rotationAnimation")
layer.addSublayer(circleLayer1)
let circleImage2 = UIImage(named: "circle_2")?.cgImage
circleLayer2.frame = self.bounds
circleLayer2.contents = circleImage2
circleLayer2.add(rotationLeft, forKey: "rotationAnimation")
layer.addSublayer(circleLayer2)
let circleImage3 = UIImage(named: "circle_3")?.cgImage
circleLayer3.frame = self.bounds
circleLayer3.contents = circleImage3
circleLayer3.add(rotationRight, forKey: "rotationAnimation")
layer.addSublayer(circleLayer3)
}
I would think something as simple as this would hide the button completely as soon as it knows it's going away:
override func viewWillDisappear(_ animated: Bool) {
animatedButton.isHidden = true
}
What's also interesting is that this seems to stop happening if I let it run for a couple minutes. This tells me that it might be some sort of race condition. Just not sure what that race might be...
When you use the UIView set of animations they set the actual state to the final state and then start the animation, ie:
UIView.animate(withDuration: 1) { view.alpha = 0 }
If you check alpha right after the animation starts, it's already 0.
This is a convenience that UIView does for you but that CALayer does not. When you set a CALayer animation you are only setting the animation value, not the actual value of the variable, so when the animation is done, the layer snaps back to its original value. Check the layer value while you are in the middle of the animation and you will see the real value has not changed; only the animated value in the presentation layer has changed.
If you want to replicate the UIView behavior you need to either set the actual final value not he layer when the animation begins or in the use the delegate to set it when the animation ends.
I'm trying to follow the example in this project to create an expand/shrink view controller transition. In my case, the view controller which is being presented will have the same background color as the button itself, therefore this animation is essentially just the button expanding to cover the whole screen without ever disappearing.
https://github.com/AladinWay/TransitionButton
I've placed my button on the bottom right of the screen and managed to get it to expand using the following function:
private func expand(completion:(()->Void)?, revertDelay: TimeInterval) {
let expandAnim = CABasicAnimation(keyPath: "transform.scale")
let expandScale = (UIScreen.main.bounds.size.height/self.frame.size.height)*2
expandAnim.fromValue = 1.0
expandAnim.toValue = max(expandScale,26.0)
expandAnim.timingFunction = expandCurve
expandAnim.duration = 0.3
expandAnim.fillMode = .forwards
expandAnim.isRemovedOnCompletion = false
CATransaction.setCompletionBlock {
completion?()
// We return to original state after a delay to give opportunity to custom transition
DispatchQueue.main.asyncAfter(deadline: .now() + revertDelay) {
self.setOriginalState(completion: nil)
self.layer.removeAllAnimations() // make sure we remove all animation
}
}
layer.add(expandAnim, forKey: expandAnim.keyPath)
CATransaction.commit()
}
Now I'm trying to create the animation for dismissing the view controller which would essentially reverse the initial animation and shrink the whole screen back to the circular button on the bottom of the screen. I'm not sure how to adapt the above code to reverse this though.
You could try using the exact same block, except switch the fromValue and toValue.
expandAnim.fromValue = max(expandScale,26.0)
expandAnim.toValue = 1.0
Essentially I would like to recreate the following animation that occurs in the Youtube app when a tab is selected.
This is currently what I have in my custom Tab Bar Controller class:
import UIKit
class AnimatedTabBarController: UITabBarController {
private var bounceAnimation: CAKeyframeAnimation = {
let bounceAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
bounceAnimation.values = [1.0, 0.9, 1.00, 1.00, 1.0]
bounceAnimation.duration = TimeInterval(0.3)
bounceAnimation.calculationMode = CAAnimationCalculationMode.cubic
return bounceAnimation
}()
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
// find index if the selected tab bar item, then find the corresponding view and get its image, the view position is offset by 1 because the first item is the background (at least in this case)
guard let idx = tabBar.items?.index(of: item), tabBar.subviews.count > idx + 1, let imageView = tabBar.subviews[idx + 1].subviews.first as? UIImageView else {
return
}
imageView.layer.add(bounceAnimation, forKey: nil)
}
}
The code above allows my tabs to bounce inwards when being selected, what can be added to the class to also add the circle and have it subtly disappear.
There are several methods you can use. You can take third party or you can create custom. In my case, I use custom.
take a viewController in storyboard
create same view as your question's images
set containerView in same viewController
give touchupinside animation in buttons or use coreGraphichs or use layers
add controller in containerView when tap.
I have two view controllers (i.e firstVC and secondVC). I am presenting secondVC over firstVC and trying to achieve rotation animation but it's not working, I have tried running animation on the main thread with some delay but it's giving weird behavior. Following is the code I am using to rotate the view.
extension UIView {
private static let kRotationAnimationKey = "rotationanimationkey"
func rotate(duration: Double = 1, delay: Int = 0) {
if layer.animation(forKey: UIView.kRotationAnimationKey) == nil {
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation")
rotationAnimation.fromValue = 0.0
rotationAnimation.toValue = Float.pi * 2.0
rotationAnimation.duration = duration
rotationAnimation.repeatCount = Float.infinity
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(delay)) {
self.layer.add(rotationAnimation, forKey: UIView.kRotationAnimationKey)
}
}
}
func stopRotating() {
if layer.animation(forKey: UIView.kRotationAnimationKey) != nil {
layer.removeAnimation(forKey: UIView.kRotationAnimationKey)
}
}}
Note: By pushing 'secondVC' animation is working properly. But I wanted to present 'secondVC'.
I am not getting what's happening during the presentation of the view controller and rotation animation.
Please let me know if you find any solution.
For using custom presentation transition you have to confirm to UIViewControllerTransitioningDelegate. UIKit always call animationController(forPresented:presenting:source:) to see if a UIViewControllerAnimatedTransitioning is returned. If it is returning nil then default animation is used.
I am using a custom path animation on UIImageView items for a Swift 3 project. The code outline is as follows:
// parentView and other parameters are configured externally
let imageView = UIImageView(image: image)
imageView.isUserInteractionEnabled = true
let gr = UITapGestureRecognizer(target: self, action: #selector(onTap(gesture:)))
parentView.addGestureRecognizer(gr)
parentView.addSubview(imageView)
// Then I set up animation, including:
let animation = CAKeyframeAnimation(keyPath: "position")
// .... eventually ....
imageView.layer.add(animation, forKey: nil)
The onTap method is declared in a standard way:
func onTap(gesture:UITapGestureRecognizer) {
print("ImageView frame is \(self.imageView.layer.visibleRect)")
print("Gesture occurred at \(gesture.location(in: FloatingImageHandler.parentView))")
}
The problem is that each time I call addGestureRecognizer, the previous gesture recognizer gets overwritten, so any detected tap always points to the LAST added image, and the location is not detected accurately (so if someone tapped anywhere on the parentView, it would still trigger the onTap method).
How can I detect a tap accurately on per-imageView basis? I cannot use UIView.animate or other methods due to a custom path animation requirement, and I also cannot create an overlay transparent UIView to cover the parent view as I need these "floaters" to not swallow the events.
It is not very clear what are you trying to achieve, but i think you should add gesture recognizer to an imageView and not to a parentView.
So this:
parentView.addGestureRecognizer(gr)
Should be replaced by this:
imageView.addGestureRecognizer(gr)
And in your onTap function you probably should do something like this:
print("ImageView frame is \(gesture.view.layer.visibleRect)")
print("Gesture occurred at \(gesture.location(in: gesture.view))")
I think you can check the tap location that belongs imageView or not on the onTap function.
Like this:
func ontap(gesture:UITapGestureRecognizer) {
let point = gesture.location(in: parentView)
if imageView.layer.frame.contains(point) {
print("ImageView frame is \(self.imageView.layer.visibleRect)")
print("Gesture occurred at \(point)")
}
}
As the layers don't update their frame/position etc, I needed to add the following in the image view subclass I wrote (FloatingImageView):
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let pres = self.layer.presentation()!
let suppt = self.convert(point, to: self.superview!)
let prespt = self.superview!.layer.convert(suppt, to: pres)
return super.hitTest(prespt, with: event)
}
I also moved the gesture recognizer to the parent view so there was only one GR at any time, and created a unique tag for each of the subviews being added. The handler looks like the following:
func onTap(gesture:UITapGestureRecognizer) {
let p = gesture.location(in: gesture.view)
let v = gesture.view?.hitTest(p, with: nil)
if let v = v as? FloatingImageView {
print("The tapped view was \(v.tag)")
}
}
where FloatingImageView is the UIImageView subclass.
This method was described in an iOS 10 book (as well as in WWDC), and works for iOS 9 as well. I am still evaluating UIViewPropertyAnimator based tap detection, so if you can give me an example of how to use UIViewPropertyAnimator to do the above, I will mark your answer as the correct one.