- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个 ViewModifier,它在其内容的右侧添加了一个图标,我希望图标显示的方式是将 .clipShape()
修饰符从 -50 设置为 0,问题是第一次出现时,它只是弹出而没有动画,最后一次消失时也会发生同样的事情。在底部你会发现一个视频演示
到目前为止我的 ViewModifier
extension View {
func addRightIcon(icon: Image, show: Bool) -> some View {
return modifier(RightIconModifier(icon: icon, show: show))
}
}
struct RightIconModifier: ViewModifier {
var icon: Image
private var iconMask: Int = 0
init(icon: Image, show: Bool) {
self.icon = icon
withAnimation(Animation.interpolatingSpring(stiffness: 170, damping: 15).delay(2.5)) {
iconMask = show ? 0 : -50
}
}
func body(content: Content) -> some View {
ZStack {
content
.overlay(rightIcon)
}
}
var rightIcon: some View {
icon
.font(.system(size: 25))
.foregroundColor(.black)
.frame(maxWidth: .infinity,
maxHeight: .infinity,
alignment: .trailing)
.padding()
.clipShape(Rectangle().offset(x: CGFloat(iconMask)))
}
}
这将是我如何使用它的一个简短版本,希望你能得到一个让它工作的想法
TextField(placeholder, text: $text).addRightIcon(icon: Image(systemName: "checkmark"), show: isTextValid)
var isTextValid: Bool {
if !text.isEmpty {
let validation = NSPredicate(format: "SELF MATCHES %@", "[’a-zA-Z]{3,20}")
let validated = validation.evaluate(with: text)
return validated
}
return false
}
最佳答案
Animatable 修饰符应该在 body 中(直接或从 body 中调用),而不是在 init 中。修饰符也是一个结构,因此如果它的属性在外部被修改,它们也是可动画的。
所以这里是固定的ViewModifier。使用 Xcode 14/iOS 16 测试
注意:出于测试目的,我简化了动画和滤镜
struct RightIconModifier: ViewModifier {
var icon: Image
var show: Bool // << injected changes
func body(content: Content) -> some View {
ZStack {
content
.overlay(rightIcon)
}
}
var rightIcon: some View {
icon
.font(.system(size: 25))
.foregroundColor(.black)
.frame(maxWidth: .infinity,
maxHeight: .infinity,
alignment: .trailing)
.padding()
.clipShape(Rectangle().offset(x: CGFloat(show ? 0 : -50))) // << switch is here !!
.animation(.easeIn(duration: 1), // << simplified for testing
value: show)
}
}
关于animation - SwiftUI ViewModifier 动画每次都无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73084339/
我的一个 View 中有这段代码: struct TextFieldClearButton: ViewModifier { @Binding var text: String func
我的一个 View 中有这段代码: struct TextFieldClearButton: ViewModifier { @Binding var text: String func
我收到一个错误“Type 'PlayButtonModifier' does not conform to protocol 'ViewModifier'”,我不明白为什么以及 - 更重要的是 - 如
我创建了一个 ViewModifier,它在其内容的右侧添加了一个图标,我希望图标显示的方式是将 .clipShape() 修饰符从 -50 设置为 0,问题是第一次出现时,它只是弹出而没有动画,最后
我正在创建一个自定义 TextField由多个装饰 View 组成的 View 。我希望能够设置内部TextField使用仅适用于该 subview 的 View 修饰符,例如键盘、大写等。 我认为最
以下代码工作正常。所以真的,我很好......但我想了解 ViewModifiers......所以我的目标是将不变的东西与动态的东西分开来创建一个 .cardify() 自定义修饰符以调用形状 Vi
有没有办法创建修改器来更新 @State private var在 View 被修改? 我有一个自定义 View ,它返回 Text带有“动态”背景颜色或 Circle具有“动态”前景色。 struc
我为 UITextField 创建了一个 UIViewRespresentable。我想应用修饰符: myTextField.font(.headline).keyboardType(keyboard
这更像是一个句法问题。我试图在不使用 AnyView 的情况下将 View 传递到 ViewModifier 初始化程序。 目前我有一些使用 AnyView 运行的东西,但我想使用通用 View 语法
这更像是一个句法问题。我试图在不使用 AnyView 的情况下将 View 传递到 ViewModifier 初始化程序。 目前我有一些使用 AnyView 运行的东西,但我想使用通用 View 语法
我有一个 ImageView ,基于一个 bool 值,我应该使用色调,如果条件为真,我应该使用色调,否则我应该使用原始图像,我尝试了多种方式使用 ViewModifier,但我找不到解决方案。是否有
我有一个自定义 ViewModifier它只是返回带有 onReceive 的相同内容修饰符,onReceive没有触发,这里有一个示例代码,您可以在XCode中复制、粘贴和运行: import Sw
我想添加一个 ViewModifier,如 Paul Hudson (https://www.hackingwithswift.com/books/ios-swiftui/custom-modifie
我试图找出这两种方法之间的实际区别。例如: struct PrimaryLabel: ViewModifier { func body(content: Content) -> some Vi
我试图找出这两种方法之间的实际区别。例如: struct PrimaryLabel: ViewModifier { func body(content: Content) -> some Vi
我在 iOS 14 上使用 SwiftlySearch,在 iOS 15 上使用 .searchable。 struct CompatibleSearchBarModifier: ViewModifi
为什么shadow()可以申请2次以上,每次更新,而对于.font()即使我提出了不同的样式参数和 .font()两次并且没有更新保持不变? struct LabelStyle: ViewModifi
我是一名优秀的程序员,十分优秀!