Swift动画删除线uilabel

时间:2019-06-21 00:20:01

标签: swift

我正在尝试通过动画来动画化罢工。我唯一能找到的帮助就是这个UIFont: how to animate strikethrough and match font style?,但是我正在尝试使用Objective-C这样的结果https://dribbble.com/shots/3167358-Microinteractions-for-to-do-list-app。但是无法弄清楚如何为删除线设置动画。

 guard let postsText = post?.post else { return }

        let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string: postsText)
        attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 1, range: NSMakeRange(0, attributeString.length))
        postLabel.attributedText = attributeString

2 个答案:

答案 0 :(得分:1)

self.mylabel.attributedText = nil;
CATransition *transition = [CATransition new];
transition.delegate = self;
transition.type = kCATransitionFromLeft;
transition.duration = 2.0f;
self.mylabel.attributedText = strikeThroughText;
[self.mylabel.layer addAnimation:transition forKey:@"transition"];

因此,我正在发布原始的目标C代码以供参考。实际上,这只是将目标C代码转换为Swift的问题,这是可能的,因为在后台,Swift可以使用与目标C可以使用的相同的核心动画api。

我使用了苹果文档作为参考。

https://developer.apple.com/documentation/quartzcore/catransition

let transition = CATransition()
transition.type = CATransitionType.moveIn
transition.subtype = CATransitionSubtype.fromLeft
transition.duration = 2.0
label.attributedText = strikeThroughText
label.layer.add(transition, forKey: kCATransition)

这应该实现类似于另一篇文章中的目标C代码的功能。您可能必须对其进行修改,但是它应该使您开始正确的路径。我将尝试使用不同的CATransitionType,然后看看可以实现什么。

答案 1 :(得分:1)

我创建了一个库,该库使用Bezier Path来创建删除线的动画效果。您可以在此处找到代码:

https://github.com/chrsp/StrikethroughLabel

作为记录,该课程显示了如何执行此操作:

https://github.com/chrsp/StrikethroughLabel/blob/master/StrikethroughLabel/StrikethroughLabel.swift