iOS的风格和Apple其他产品一样,简单而粗暴。没有给人其他选择的余地,让你又爱又恨。同样的,Apple对待iOS平台的开发人员和对待大众消费者一样,也不给你留余地。UIAlertView就是一个鲜明标志。功能简单,甚至单一,定制性差,消耗资源。在资源紧张的设备上,UIAlertView的动画效果都会稍微卡一下,很是别扭。
这时还是很希望在iOS上有一个Android风格的Toast控件。
我制作了一个demo,使用起来感觉效果还是很不错的,简单易用,在这里和大家分享一下。(swift3.0)
import UIKit
enum JHToastShowType {
case JHToastShowTypeTop,
JHToastShowTypeCenter,
JHToastShowTypeBottom
}
let kDefaultForwardAnimationDuration:CFTimeInterval =
let kDefaultBackwardAnimationDuration:CFTimeInterval =
let kDefautlWaitAnimationDuration:CFTimeInterval =
let kDefaultTopMargin:CGFloat =
let kDefaultTextInset:CGFloat =
class JHToast: UILabel,CAAnimationDelegate {
private var forwardAnimationDuration : CFTimeInterval!
private var backwardAnimationDuration : CFTimeInterval!
private var textInsets : UIEdgeInsets!
private var maxWidth : CGFloat!
override func awakeFromNib() {
super.awakeFromNib()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.forwardAnimationDuration = kDefaultForwardAnimationDuration
self.backwardAnimationDuration = kDefaultBackwardAnimationDuration
self.textInsets = UIEdgeInsetsMake(kDefaultTextInset, kDefaultTextInset, kDefaultTextInset, kDefaultTextInset)
self.maxWidth = UIScreen.main.bounds.size.width -
self.layer.cornerRadius =
self.layer.masksToBounds = true
self.backgroundColor = UIColor.black.withAlphaComponent()
self.numberOfLines =
self.textAlignment = .center
self.textColor = UIColor.white
self.font = UIFont.systemFont(ofSize: )
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
fatalError("init(coder:) has not been implemented")
}
func showToast(text:String, type:JHToastShowType) {
self.text = text
self.sizeToFit()
self.showType(type: type)
}
private func showType(type:JHToastShowType) {
let keyWindow = UIApplication.shared.delegate?.window
self.addAnimationGroup()
var point = keyWindow??.center
switch type {
case .JHToastShowTypeTop:
point?.y = kDefaultTopMargin
break
case .JHToastShowTypeCenter:
break
case .JHToastShowTypeBottom:
point?.y = (keyWindow??.bounds.size.height)! - kDefaultTopMargin
break
}
self.center = point!
keyWindow??.addSubview(self)
}
/** Animation */
private func addAnimationGroup() {
let forwardAnimation = CABasicAnimation.init(keyPath: "transform.scale")
forwardAnimation.duration = self.forwardAnimationDuration
forwardAnimation.timingFunction = CAMediaTimingFunction.init(controlPoints: , , , )
forwardAnimation.fromValue = NSNumber.init(value: )
forwardAnimation.toValue = NSNumber.init(value: )
let backwardAnimation = CABasicAnimation.init(keyPath: "transform.scale")
backwardAnimation.duration = self.backwardAnimationDuration
backwardAnimation.beginTime = forwardAnimation.duration + kDefautlWaitAnimationDuration
backwardAnimation.timingFunction = CAMediaTimingFunction.init(controlPoints: , , , -)
backwardAnimation.fromValue = NSNumber.init(value: )
backwardAnimation.toValue = NSNumber.init(value: )
let animationGroup = CAAnimationGroup()
animationGroup.animations = [forwardAnimation,backwardAnimation]
animationGroup.duration = forwardAnimation.duration + backwardAnimation.duration + kDefautlWaitAnimationDuration
animationGroup.isRemovedOnCompletion = false
animationGroup.delegate = self
animationGroup.fillMode = kCAFillModeForwards
self.layer.add(animationGroup, forKey: "customShow")
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
if flag {
self.removeFromSuperview()
self.layer.removeAnimation(forKey: "customShow")
}
}
/** text */
override func sizeToFit() {
super.sizeToFit()
var frame = self.frame
let width = self.bounds.size.width + self.textInsets.left + self.textInsets.right
frame.size.width = min(width, self.maxWidth)
frame.size.height = self.bounds.size.height + self.textInsets.top + self.textInsets.bottom
self.frame = frame
}
override func draw(_ rect: CGRect) {
super.drawText(in: UIEdgeInsetsInsetRect(rect, self.textInsets))
}
override func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect {
let size = self.text?.boundingRect(
with: CGSize.init(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
options: .usesLineFragmentOrigin,
attributes: [NSFontAttributeName:self.font],
context:nil).size
let bound = CGRect.init(origin: bounds.origin, size: size!)
return bound
}
}
Github上有完整代码,喜欢的给颗星哦,谢谢!
下载代码JHToast