AMPopTip
AMPopTip 是一个优雅的动画弹出提示框,可以从指定框架弹出。您可以指定弹出方向以及指向其原点的箭头。颜色、边框半径和字体都可以轻松自定义。此弹出提示框可用于为您的 UI 提供微妙的提示,并提供有趣的引导弹窗。
功能特性
- 多种弹出方向:支持上、下、左、右及无箭头居中显示
- 高度可定制:轻松自定义颜色、边框半径、字体等外观属性
- 流畅动画:提供平滑的显示和隐藏动画效果
- 简单易用的API:几行代码即可实现复杂的提示效果
- 自动布局适应:智能适应不同屏幕尺寸和方向
- Swift编写:基于 Swift 的现代化实现,性能优异
安装指南
CocoaPods
在 Podfile
中添加:
pod 'AMPopTip'
然后运行:
pod install
open App.xcworkspace
Carthage
在 Cartfile
中添加:
github "andreamazz/AMPopTip"
然后运行:
carthage update
将 AMPopTip.framework
添加到项目中,并在代码中导入:
import AMPopTip
使用说明
基本用法
显示弹出提示非常简单:
let popTip = PopTip()
popTip.show(text: "嘿!看这里!", direction: .up, maxWidth: 200, in: view, from: someView.frame)
无箭头居中显示
popTip.show(text: "嘿!看这里!", direction: .none, maxWidth: 200, in: view, from: view.frame)
自定义外观
let popTip = PopTip()
popTip.bubbleColor = UIColor.blue
popTip.textColor = UIColor.white
popTip.cornerRadius = 10
popTip.font = UIFont.systemFont(ofSize: 14)
popTip.show(text: "自定义样式提示", direction: .down, maxWidth: 200, in: view, from: button.frame)
核心代码
以下是 AMPopTip 的核心实现代码:
// 主要显示方法
public func show(text: String, direction: PopTipDirection, maxWidth: CGFloat, in view: UIView, from frame: CGRect) {self.text = textself.direction = directionself.containerView = viewself.from = frame// 配置外观configureBubble()configureText()// 计算位置calculatePosition()// 添加动画addShowAnimation()
}
// 气泡配置
private func configureBubble() {bubbleView.backgroundColor = bubbleColorbubbleView.layer.cornerRadius = cornerRadiusbubbleView.layer.shadowColor = UIColor.black.cgColorbubbleView.layer.shadowOffset = CGSize(width: 0, height: 2)bubbleView.layer.shadowRadius = 3bubbleView.layer.shadowOpacity = 0.1
}
// 箭头绘制
private func drawArrow() {let arrowPath = UIBezierPath()switch direction {case .up:arrowPath.move(to: CGPoint(x: arrowPosition.x, y: arrowPosition.y))arrowPath.addLine(to: CGPoint(x: arrowPosition.x - arrowSize, y: arrowPosition.y + arrowSize))arrowPath.addLine(to: CGPoint(x: arrowPosition.x + arrowSize, y: arrowPosition.y + arrowSize))case .down:arrowPath.move(to: CGPoint(x: arrowPosition.x, y: arrowPosition.y))arrowPath.addLine(to: CGPoint(x: arrowPosition.x - arrowSize, y: arrowPosition.y - arrowSize))arrowPath.addLine(to: CGPoint(x: arrowPosition.x + arrowSize, y: arrowPosition.y - arrowSize))// 其他方向...}arrowPath.close()bubbleColor.setFill()arrowPath.fill()
}
// 显示动画
private func addShowAnimation() {transform = CGAffineTransform(scaleX: 0.1, y: 0.1)alpha = 0UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.8, options: .curveEaseOut, animations: {self.transform = .identityself.alpha = 1})
}
AMPopTip 提供了完整的提示框解决方案,通过简洁的 API 和丰富的自定义选项,让开发者能够轻松创建专业的用户引导和界面提示效果。
更多精彩内容 请关注我的个人公众号 公众号(办公AI智能小助手)
对网络安全、黑客技术感兴趣的朋友可以关注我的安全公众号(网络安全技术点滴分享)
公众号二维码
公众号二维码