实例介绍
【实例简介】通过拖入工程,OC采用混编,swift可直接引用,方便调用
【实例截图】
import Foundation
import UIKit
public func kRGB(r:CGFloat, g:CGFloat, b:CGFloat) ->UIColor{return UIColor (red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1.0)}
let AppleAppID = "1291661719"
///屏幕宽
let kSCREEN_WIDTH = UIScreen.main.bounds.width
///屏幕高
let kSCREEN_HEIGHT = UIScreen.main.bounds.height
let Ratio_375 = kSCREEN_WIDTH / 375.0
/// 转换成当前比例的数
func Ratio(x:CGFloat) -> CGFloat {
return CGFloat(x * Ratio_375)
}
let DEFAULT_MAX_HEIGHT = kSCREEN_HEIGHT / 3 * 2
class AppUpdateAlert:UIView {
/** 版本号 */
var version = ""
/** 版本更新内容 */
var desc = ""
/** 短连接 */
var shortUrl = ""
@objc class func showUpdateAlert(version: String?, description: String?, shortUrl:String?) {
let updateAlert = AppUpdateAlert.init(version: version!, description: description!, shortUrl:shortUrl!)
UIApplication.shared.delegate?.window??.addSubview(updateAlert)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(version:String,description:String, shortUrl:String) {
super.init(frame: UIScreen.main.bounds)
self.version = version
desc = description
self.shortUrl = shortUrl
initView()
}
}
// MARK: - init
extension AppUpdateAlert{
func initView() {
self.frame = UIScreen.main.bounds
self.backgroundColor = UIColor(red: CGFloat(0 / 255.0), green: CGFloat(0 / 255.0), blue: CGFloat(0 / 255.0), alpha: 0.3 / 1.0)
//获取更新内容高度
var descHeight = _sizeofString(desc, font: UIFont.systemFont(ofSize: 16), maxSize: CGSize(width: self.frame.size.width-Ratio(x: 136), height: 1000)).height
//bgView实际高度
let realHeight: CGFloat = descHeight Ratio(x: 314)
//bgView最大高度
var maxHeight: CGFloat = DEFAULT_MAX_HEIGHT
//更新内容可否滑动显示
var scrollEnabled = false
//重置bgView最大高度 设置更新内容可否滑动显示
if realHeight > DEFAULT_MAX_HEIGHT {
scrollEnabled = true
descHeight = DEFAULT_MAX_HEIGHT - Ratio(x: 314)
} else {
maxHeight = realHeight
}
let bgView = UIView()
bgView.center = center
bgView.bounds = CGRect(x: 0, y: 0, width: frame.size.width - Ratio(x: 40), height: maxHeight Ratio(x: 18))
addSubview(bgView)
//添加更新提示
let updateView = UIView(frame: CGRect(x: Ratio(x: 20), y: Ratio(x: 18), width: bgView.frame.size.width - Ratio(x: 40), height: maxHeight))
updateView.backgroundColor = UIColor.init(white: 1, alpha: 0);
bgView.addSubview(updateView)
let updatebg_image = UIImageView(frame: CGRect(x: 0, y: 0, width: updateView.frame.size.width, height: updateView.frame.size.height))
updatebg_image.image = UIImage(named: "update_bg")
updateView.addSubview(updatebg_image)
let updateIcon = UIImageView(frame: CGRect(x: (updateView.frame.size.width/8)*3, y: -Ratio(x: (updateView.frame.size.width)/4), width: Ratio(x: (updateView.frame.size.width)/4), height: Ratio(x: (updateView.frame.size.width)/2)))
updateIcon.image = UIImage(named: "update_huojian")
updateView.addSubview(updateIcon)
//版本号
let versionLabel = UILabel(frame: CGRect(x: Ratio(x:25), y: Ratio(x:160), width: updateView.frame.size.width, height: Ratio(x:28)))
versionLabel.font = UIFont.boldSystemFont(ofSize: 16)
versionLabel.textAlignment = .left
versionLabel.text = "发现新版本 V\(version)"
updateView.addSubview(versionLabel)
let descTextView = UITextView(frame: CGRect(x: Ratio(x: 28), y: Ratio(x: 10) versionLabel.frame.maxY, width: updateView.frame.size.width - Ratio(x:56), height: descHeight))
descTextView.font = UIFont.systemFont(ofSize: CGFloat(16))
descTextView.textContainer.lineFragmentPadding = 0
descTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0)
descTextView.text = desc
descTextView.isEditable = false
descTextView.isSelectable = false
descTextView.isScrollEnabled = scrollEnabled
descTextView.showsVerticalScrollIndicator = scrollEnabled
descTextView.showsHorizontalScrollIndicator = false
updateView.addSubview(descTextView)
if scrollEnabled {
//若显示滑动条,提示可以有滑动条
descTextView.flashScrollIndicators()
}
//更新按钮
let updateButton = UIButton(type: .system)
updateButton.backgroundColor = kRGB(r: 239, g: 33, b: 67)
updateButton.frame = CGRect(x: Ratio(x:25), y: descTextView.frame.maxY Ratio(x:20), width: updateView.frame.size.width - Ratio(x:50), height: Ratio(x:40))
updateButton.clipsToBounds = true
updateButton.layer.cornerRadius = 2.0
updateButton.addTarget(self, action: #selector(self.updateVersion), for: .touchUpInside)
updateButton.setTitle("立即更新", for: .normal)
updateButton.setTitleColor(UIColor.white, for: .normal)
updateView.addSubview(updateButton)
//取消按钮
let cancelButton = UIButton(type: .system)
cancelButton.alpha = 0.0;
cancelButton.backgroundColor = UIColor.white
cancelButton.frame = CGRect(x: Ratio(x: 25), y: updateButton.frame.maxY Ratio(x:5), width: updateView.frame.size.width - Ratio(x:50), height: Ratio(x:30))
cancelButton.setTitle("取消", for: .normal)
cancelButton.setTitleColor(UIColor.darkGray, for: .normal)
cancelButton.addTarget(self, action: #selector(self.cancelAction), for: .touchUpInside)
updateView.addSubview(cancelButton)
//显示更新
show(withAlert: bgView)
}
}
// MARK: - action
extension AppUpdateAlert{
/// 更新按钮点击事件 跳转AppStore更新
@objc func updateVersion() {
let str = self.shortUrl
if let aStr = URL(string: str) {
UIApplication.shared.openURL(aStr)
}
}
/// 取消按钮点击事件
@objc func cancelAction() {
dismissAlert()
}
/// 添加Alert入场动画
///
/// - Parameter alert: 添加动画的View
@objc func show(withAlert alert: UIView?) {
let animation = CAKeyframeAnimation(keyPath: "transform")
animation.duration = CFTimeInterval(0.3)
var values = [AnyHashable]()
values.append(NSValue(caTransform3D: CATransform3DMakeScale(0.1, 0.1, 1.0)))
values.append(NSValue(caTransform3D: CATransform3DMakeScale(1.2, 1.2, 1.0)))
values.append(NSValue(caTransform3D: CATransform3DMakeScale(0.9, 0.9, 1.0)))
values.append(NSValue(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)))
animation.values = values
alert?.layer.add(animation, forKey: nil)
}
/// 添加Alert出场动画
@objc func dismissAlert() {
UIView.animate(withDuration: 0.3, animations: {() -> Void in
self.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
self.backgroundColor = UIColor.clear
self.alpha = 0
}, completion: {(_ finished: Bool) -> Void in
self.removeFromSuperview()
})
}
@objc func _sizeofString(_ string: String?, font: UIFont?, maxSize: CGSize) -> CGSize {
let attributes = [NSAttributedStringKey.font: font]
let rect:CGRect = string?.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: attributes as Any as? [NSAttributedStringKey : Any], context: nil) ?? CGRect.zero
return rect.size
}
}
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论