UITapGestureRecognizer Activate on Immediate Touch? - ios

The title might be a little confusing, but is there a way I can Get this code to execute the moment I touch the uiview?
entry.transform = CGAffineTransform(scaleX: 0.975, y: 0.975)
speech.transform = CGAffineTransform(scaleX: 0.975, y: 0.975)
I have a UIView that I want to simulate a button press. Currently, the way I have it set up, It only triggers once my finger has left the screen
func voiceTap(sender: UITapGestureRecognizer? = nil) {
entry.transform = CGAffineTransform(scaleX: 0.975, y: 0.975)
speech.transform = CGAffineTransform(scaleX: 0.975, y: 0.975)
UIView.animate(withDuration: 0.2, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
self.entry.transform = CGAffineTransform.identity
self.speech.transform = CGAffineTransform(scaleX: 0.975, y: 0.975)
}, completion: nil)
}
How to I get the code outside of the animation block to trigger the moment my finger touches the UIView?

You need to implement the methods (touchesBegan: withEvent: etc) to detect the received touch in your UIViewController and check if the touch corresponds to the UIView of your interest. And from there you can trigger an action.
Check this thread how to implement the methods
Or this one

Related

iOS Animation is not working after switch to another VC Swift

I have small background animation to change gradient, ex u can see here animation
As you see if i open app first time, animation is working, after the changing View Controller animation is stop.
My code:
func animateGrandient() {
UIView.animate(withDuration: 15, delay: 0, options: [.autoreverse, .curveLinear, .repeat], animations: {
let x = -(self.gradientView.frame.width - self.view.frame.width)
self.gradientView.transform = CGAffineTransform(translationX: x, y: 0)
})
}
And outlet:
#IBOutlet weak var gradientView: UIImageView!
This happens because your self.gradientView.transform is changed already before your animation is executed, so you need to reset your self.gradientView.transform
Add this line self.gradientView.transform = CGAffineTransform.identity in the beginning of that method
fixed code
func animateGrandient() {
self.gradientView.transform = CGAffineTransform.identity
UIView.animate(withDuration: 15, delay: 0, options: [.autoreverse, .curveLinear, .repeat], animations: {
let x = -(self.gradientView.frame.width - self.view.frame.width)
self.gradientView.transform = CGAffineTransform(translationX: x, y: 0)
})
}

change button's scale and add a shadow when click button

I want to change the button's scale and add a shadow effect when the button is clicked, but the added shadow effect is not complete, where is the problem, why is the shadow of the last button only complete?
#objc func ButtonOnClicking(_ sender:homePageBtn){
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.allowAnimatedContent, animations: {
sender.layer.masksToBounds = false
sender.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
sender.layer.shadowColor = ColorHellp.getColor("333333").cgColor
sender.layer.shadowOffset = CGSize(width: 0, height: 0)
sender.layer.shadowRadius = 4
sender.layer.shadowOpacity = 0.3
}) { (isfinished) in
print("finished")
}
}
I can see, that also for the first button the shadow is complete, but it looks like, that the button is under the other ones.
So try something like bringSubViewToFront or change the z-index.

UIView.animateKeyframes jumps to start of next animation instead of transitioning smoothly

I'm trying to create an animation where an object moves from one keypoint to the next smoothly. As of right now the object does move to the different key points but instead of transitioning to each one as soon as it finishes one animation it automatically jumps to the next keypoint but with no transition animation in between. This is the function I'm using:
func keyframeAnimate () {
UIView.animateKeyframes(withDuration: 3, delay: 0, options: [], animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1.0, animations: {
let translation = CGAffineTransform(translationX: 150, y: 150)
self.square.alpha = 0.5
self.square.transform = translation.rotated(by: CGFloat.pi).scaledBy(x: 2, y: 2)
})
UIView.addKeyframe(withRelativeStartTime: 1.01, relativeDuration: 1.0, animations: {
let translation = CGAffineTransform(translationX: 100, y: 400)
self.square.alpha = 1
self.square.transform = translation.rotated(by: -CGFloat.pi).scaledBy(x: 1, y: 1)
})
})
}
Any help is appreciated!
Your problem is in the relative start and relative duration. As the documentation explains, these parameters should be values between 0 and 1 because these are fractions of the entire animateKeyframes(withDuration: duration

Swift - animating UIImageView image

just a quick issue I'm having regarding animating a moving background image for my current application setup. Essentially I want to have my currently still background image scroll from left to right endlessly across my view; but I'm struggling piecing it all together. From a variety of various other sources I've comprised this function that isn't currently working, and i really can't figure out how or why.
func animate(_ image: UIImageView) {
UIView.animate(withDuration: 5, delay: 0, options: .curveLinear, animations: {
image.transform = CGAffineTransform(translationX: -100, y: 0)
}) { (success: Bool) in
image.transform = CGAffineTransform.identity
self.animate(image)
}
}
Any help/assistance is immensely appreciated!
You don't need to use CAffineTranform inside animateWithDuration, you can simply define the new center like:
let oldCenter = image.center
let newCenter = CGPoint(x: oldCenter.x - 100, y: oldCenter.y)
UIView.animate(withDuration: 1, delay: 0, options: .curveLinear, animations: {
image.center = newCenter
}) { (success: Bool) in
print("Done moving image")
}
Hope it helps.

Xcode Swift - Animating UIImageView Repetitively

just quick inquiry in regards to implementing an issue I'm having with animating a UIImageView. I successfully implemented animating an image to slide off screen; but i want it to reappear when it exists the view to simulate a side-scroller game animation.
Something like this:
I've tried implementing a completion handler but struggled understanding the logic of how to implement it, so I removed my attempts; so my code is left as follows:
let oldCenter = background.center
let newCenter = CGPoint(x: oldCenter.x - 400, y: oldCenter.y)
UIView.animate(withDuration: 2, delay: 0, options: .curveLinear, animations: {
self.background.center = newCenter
}) { (success: Bool) in
print("Done moving image")
}
An example or pointers on how to achieved my desired animation would be appreciated!
let oldCenter = view.center
let newCenter = CGPoint(x: oldCenter.x - 400, y: oldCenter.y)
UIView.animate(withDuration: 2.0, delay: 0.0, options:
[.curveLinear, .repeat], animations: {
view.center = newCenter
}, completion: nil)
If you want to repeat the animation,
there is no need to implement the completion.
animate(withDuration:delay:options:animations:completion:) has options parameter (UIViewAnimationOptions), which is:
A mask of options indicating how you want to perform the animations.
For a list of valid constants, see UIViewAnimationOptions.
One of the constants for the UIViewAnimationOptions is repeat:
Repeat the animation indefinitely.
So, what you should do:
UIView.animateKeyframes(withDuration: 2.0, delay: 0.0, options: [.curveLinear, .repeat], animations: {
self.background.center = newCenter
}, completion: nil)
Again, implementing completion is not required for repeating the animation, implementing it is up to your case.
You can try UIView UIView repeat option
let oldCenter = background.center
let newCenter = CGPoint(x: oldCenter.x - 400, y: oldCenter.y)
UIView.animate(withDuration: 2, delay: 0, options: [.curveLinear, repeat], animations: {
self.background.center = newCenter
}) { (success: Bool) in
print("Done moving image")
if(success)
{
self.background.center = oldCenter
}
}

Resources