I have a dynamically created list of labels and I am using GestureRecognizer
for each of the labels to detect touches/click on them. I couldn't use UIButton
as I wanted to pass some text which is unique to each label to the touch event handler and UIButton
would not allow text to be passed as userinfo
. I cannot use UIButton.tag
to pass the additional info.
Now I want the UIButton
like glow effect on touch on my UIlabel
. If there are other ways to notify a user that a label was touched, that work 开发者_StackOverflowtoo. I was alo thinking of using some kind of quick animation or jiggling effect. Any ideas or workarounds?
Thanks in advance.
Why dont you create buttons programmatically and make the button type as custom. Like so -
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setBackgroundImage:[[UIImage imageNamed:@"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton setTitle:@"My Button"];
[sampleButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];
Here we have defined what the button should look like UIControlStateNormal
. You can define what it should look like for UIControlStateHighlighted
.
What i want to say is that using uibuttons you can get what you need. Dont have to use uilabels.
Swift 4
1- Create UIView extension with the animation
2- Use it on textfields, buttons, views (any subclass of UIView)
UIView extension
import UIKit
extension UIView{
enum GlowEffect:Float{
case small = 0.4, normal = 1, big = 5
}
func doGlowAnimation(withColor color:UIColor, withEffect effect:GlowEffect = .normal) {
layer.masksToBounds = false
layer.shadowColor = color.cgColor
layer.shadowRadius = 0
layer.shadowOpacity = effect.rawValue
layer.shadowOffset = .zero
let glowAnimation = CABasicAnimation(keyPath: "shadowRadius")
glowAnimation.fromValue = 0
glowAnimation.toValue = 1
glowAnimation.beginTime = CACurrentMediaTime()+0.3
glowAnimation.duration = CFTimeInterval(0.3)
glowAnimation.fillMode = kCAFillModeRemoved
glowAnimation.autoreverses = true
glowAnimation.isRemovedOnCompletion = true
layer.add(glowAnimation, forKey: "shadowGlowingAnimation")
}
}
How to use it:
//Button
button.doGlowAnimation(withColor: UIColor.red, withEffect: .big)
精彩评论