iOS粒子特效、仿微信朋友圈、转场动画、抢红包动画等源码
iOS源码分享
1. 粒子特效粒子特效是一种常见的视觉效果,用于表示系统正在进行某些操作,如加载、缓冲或提示用户。下面是实现粒子特效的示例代码:
```swiftimport UIKitclass ParticleEffectView: UIView {
var particles = [Particle]()
override func draw(_ rect: CGRect) {
super.draw(rect)
// 创建粒子数组 for _ in0...100 {
let particle = Particle()
particle.position = CGPoint(x: rect.midX, y: rect.midY)
particle.velocity = CGPoint(x: CGFloat.random(in: -5...5), y: CGFloat.random(in: -5...5))
particles.append(particle)
}
// 开始绘制 for particle in particles {
drawParticle(at: particle.position, with: particle.velocity)
}
}
func drawParticle(at position: CGPoint, with velocity: CGPoint) {
let path = UIBezierPath()
path.move(to: position)
path.addArc(withCenter: position, radius:5, startAngle:0, endAngle: .pi *2, clockwise: true)
// 绘制粒子 UIColor.white.withAlphaComponent(0.5).setFill()
path.fill()
}
class Particle {
var position = CGPoint.zero var velocity = CGPoint.zero func update() {
position.x += velocity.x position.y += velocity.y // 粒子超出边界时重置 if position.x < 0 || position.x > bounds.width {
position.x = bounds.width /2 velocity.x *= -1 }
if position.y < 0 || position.y > bounds.height {
position.y = bounds.height /2 velocity.y *= -1 }
}
}
}
```
2.仿微信朋友圈下面是实现仿微信朋友圈效果的示例代码:
```swiftimport UIKitclass CircleOfFriendsDisplay: UIView {
var friends = [Friend]()
override func draw(_ rect: CGRect) {
super.draw(rect)
// 创建好友数组 for _ in0...10 {
let friend = Friend()
friend.position = CGPoint(x: CGFloat.random(in:0...rect.width), y: CGFloat.random(in:0...rect.height))
friends.append(friend)
}
// 开始绘制 for friend in friends {
drawFriend(at: friend.position, with: friend.name)
}
}
func drawFriend(at position: CGPoint, with name: String) {
let path = UIBezierPath()
path.move(to: position)
path.addArc(withCenter: position, radius:20, startAngle:0, endAngle: .pi *2, clockwise: true)
// 绘制好友头像 UIColor.white.withAlphaComponent(0.5).setFill()
path.fill()
// 绘制好友昵称 let label = UILabel(frame: CGRect(x: position.x -50, y: position.y +30, width:100, height:20))
label.text = name label.font = UIFont.systemFont(ofSize:14)
label.textAlignment = .center addSubview(label)
}
class Friend {
var position = CGPoint.zero var name = ""
func update() {
// 好友位置随机移动 position.x += CGFloat.random(in: -5...5)
position.y += CGFloat.random(in: -5...5)
// 超出边界时重置 if position.x < 0 || position.x > bounds.width {
position.x = bounds.width /2 }
if position.y < 0 || position.y > bounds.height {
position.y = bounds.height /2 }
}
}
}
```
3. 转场动画下面是实现转场动画效果的示例代码:
```swiftimport UIKitclass TransitionAnimation: UIView {
override func didMoveToSuperview() {
super.didMoveToSuperview()
// 创建转场动画 let animation = CATransition()
animation.duration =0.5 animation.type = .fade layer.add(animation, forKey: nil)
}
}
```
4. 抢红包动画下面是实现抢红包动画效果的示例代码:
```swiftimport UIKitclass RedPacketAnimation: UIView {
override func didMoveToSuperview() {
super.didMoveToSuperview()
// 创建抢红包动画 let animation = CAKeyframeAnimation(keyPath: "transform")
animation.duration =0.5 animation.values = [CATransform3DIdentity, CATransform3DMakeScale(1.2,1.2)]
layer.add(animation, forKey: nil)
}
}
```
5. 视频添加水印及粒子特效下面是实现视频添加水印及粒子特效的示例代码:
```swiftimport AVFoundationclass VideoPlayer: UIView {
var videoPlayer = AVPlayer()
override func didMoveToSuperview() {
super.didMoveToSuperview()
// 创建视频播放器 let url = URL(fileURLWithPath: "video.mp4")
videoPlayer = AVPlayer(url: url)
videoPlayer.play()
// 添加水印 let watermark = CALayer()
watermark.contents = UIImage(named: "watermark")?.cgImage watermark.frame = CGRect(x:0, y:0, width: bounds.width, height:50)
layer.addSublayer(watermark)
// 添加粒子特效 let particleEffect = CALayer()
particleEffect.contents = UIImage(named: "particle")?.cgImage particleEffect.frame = CGRect(x:0, y:0, width: bounds.width, height: bounds.height)
layer.addSublayer(particleEffect)
}
}
```
6. 小红点(消息推送提醒)
下面是实现小红点(消息推送提醒)的示例代码:
```swiftimport UIKitclass RedDotNotification: UIView {
override func didMoveToSuperview() {
super.didMoveToSuperview()
// 创建小红点 let redDot = CALayer()
redDot.contents = UIImage(named: "red_dot")?.cgImage redDot.frame = CGRect(x:0, y:0, width:20, height:20)
layer.addSublayer(redDot)
// 添加动画效果 let animation = CABasicAnimation(keyPath: "transform")
animation.duration =0.5 animation.fromValue = CATransform3DIdentity animation.toValue = CATransform3DMakeScale(1.2,1.2)
redDot.add(animation, forKey: nil)
}
}
```
以上是实现iOS粒子特效、仿微信朋友圈、转场动画、抢红包动画等源码的示例代码。