【swift】SwiftUI 动画
分类:
技术
简介:🔹 SwiftUI 动画SwiftUI 提供声明式动画,核心就是 .animation 修饰符和 withAnimation。1. 隐式动画(Implicit Animation)只要状态值发生变化,UI 会自动过渡。struct ImplicitAnimationView: View {
@State private var isScaled = false
var body: some View {
VStack {
Circle()
.fill(Color.blue)
.frame(width: isScaled ? 200 : 100, height: isScaled ? 200 : 100)
.animation(.easeInOut(duration: 1), value: isScaled) // 隐式动画
Button("切换") {
isScaled.toggle()
}
}
}
}2. 显式动画(Explicit Animation)用 withAnimation { ... } 包裹状态变化。struct ExplicitAnimationView: View {
@State private var isRotated = false
var body: some View {
VStack {
Rectangle()
.fill(Color.red)
.frame(width: 100, height: 100)
.rotationEffect(.degrees(isRotated ? 180 : 0))
Button("旋转") {
withAnimation(.spring(response: 0.5, dampingFraction: 0.4)) {
isRotated.toggle()
}
}
}
}
}3. 过渡动画(Transition Animation)针对视图的 插入和删除。struct TransitionAnimationView: View {
@State private var showBox = false
var body: some View {
VStack {
if showBox {
Rectangle()
.fill(Color.green)
.frame(width: 150, height: 150)
.transition(.scale) // 进入/退出动画
}
Button("切换") {
withAnimation(.easeInOut) {
showBox.toggle()
}
}
}
}
}4. 动画修饰器(Animation Modifier)绑定到某个值,值变化时触发动画。struct AnimationModifierView: View {
@State private var offsetX: CGFloat = 0
var body: some View {
Circle()
.frame(width: 80, height: 80)
.offset(x: offsetX)
.onTapGesture {
offsetX = offsetX == 0 ? 200 : 0
}
.animation(.easeInOut(duration: 1), value: offsetX) // 绑定值
}
}🔹 UIKit 动画在 UIKit 里,主要靠 UIView.animate 和 核心动画(Core Animation)。1. UIView.animateUIView.animate(withDuration: 0.5, animations: {
someView.alpha = 0.0
someView.frame.origin.y += 100
}) { finished in
print("动画完成")
}2. 弹簧动画UIView.animate(withDuration: 0.8,
delay: 0,
usingSpringWithDamping: 0.5,
initialSpringVelocity: 0.5,
options: [],
animations: {
someView.transform = CGAffineTransform(scaleX: 2, y: 2)
},
completion: nil)3. 核心动画(CAAnimation)适合更复杂的场景。let animation = CABasicAnimation(keyPath: "position.x")
animation.fromValue = 0
animation.toValue = 200
animation.duration = 1.0
someView.layer.add(animation, forKey: "moveX")总结SwiftUI → 用 .animation、withAnimation、transition。UIKit → 用 UIView.animate、UIViewPropertyAnimator、CAAnimation。
【swift】通用获取当前系统的语言
分类:
技术
简介:LanguageHelper.swift import Foundation
struct LanguageHelper {
/// 获取当前系统语言代码 (ISO 639 1)
static func currentLanguageCode() > String {
return Locale.current.language.languageCode?.identifier ?? "未知"
}
/// 获取当前系统地区 (ISO 3166 1)
static func currentRegionCode() > String {
return Locale.current.region?.identifier ?? "未知"
}
/// 获取 App 首选语言(取决于 Info.plist 里配置的 Localizations)
static func appPreferredLanguage() > String {
return Bundle.main.preferredLocalizations.first ?? "未知"
}
/// 获取系统语言优先级列表
static func systemPreferredLanguages() > [String] {
return Locale.preferredLanguages
}
/// 打印常用信息
static func debugPrintLanguages() {
print("🌐 系统语言代码: \(currentLanguageCode())")
print("📍 系统地区代码: \(currentRegionCode())")
print("📱 App 首选语言: \(appPreferredLanguage())")
print("🗂 系统语言优先级列表: \(systemPreferredLanguages())")
}
}
调用 LanguageHelper.debugPrintLanguages()
// 也可以单独调用
let lang = LanguageHelper.currentLanguageCode()
let region = LanguageHelper.currentRegionCode()
GitHub推送失败解决记录
分类:
技术
简介:报错部分代码remote: Invalid username or token. Password authentication is not supported for Git operations.解决步骤生成GitHub专用密钥 ssh keygen t ed25519 C "你的GitHub邮箱" f ~/.ssh/id_ed25519_github查看并复制公钥 cat ~/.ssh/id_ed25519_github.pub在GitHub上面保存公钥测试连接 ssh T git@github.com其他错误SSH 服务未启动或端口被屏蔽Connection closed by...port 22解决步骤编辑 ~/.ssh/config 文件(如果没有,可以创建一个) nano ~/.ssh/config添加以下内容 Host github.com
HostName ssh.github.com
User git
Port 443SSH 密钥未正确加载、GitHub 没有正确添加公钥、或者配置不正确等git@ssh.github.com: Permission denied (publickey)解决步骤先使用下面的命令查看 SSH 代理是否已加载你的私钥 ssh add l ssh add ~/.ssh/id_ed25519_github