我正在尝试在iOS应用中的UIImageView上实现持续时间的闪烁效果。我希望UIImageView用Alpha而不是其他图像闪烁/闪烁。
我有一个来自android应用程序的代码片段,它描述了我要在XCode Swift中实现的目标。我将其发布在下面:
这是我到目前为止在XCode Swift中所实现的:
ellipses.alpha=0.8
ellipses.animationDuration=1
ellipses.animationRepeatCount=0
ellipses.startAnimating()
当然,它不起作用!我不确定如何实现这一目标,很高兴获得一些指导。
下面的代码适用于android,不适用于Swift / Xcode (但这说明了我的目标很明确)
Animation animation = new AlphaAnimation(1, 0); //to change visibility from visible to invisible
animation.setDuration(1000); //1 second duration for each animation cycle
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(Animation.INFINITE); //repeating indefinitely
animation.setRepeatMode(Animation.REVERSE); //animation will start from end point once ended.
imageButton.startAnimation(animation); //to start animation
答案 0 :(得分:5)
您可以反复为图像视图的不透明度设置动画。像这样:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.core:core-ktx:1.1.0-alpha05'
implementation 'androidx.cardview:cardview:1.0.0' // Included
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0-alpha05'
implementation 'com.jakewharton.timber:timber:4.7.1'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.nabinbhandari.android:permissions:3.8'
implementation project(':volley-master')
testImplementation 'junit:junit:4.12'
}
答案 1 :(得分:3)
使用以下代码来获取图像动画效果。
图像闪烁(使用图像视图隐藏/显示):-
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.alarmAlertActivate), userInfo: nil, repeats: true)
}
@objc func alarmAlertActivate(){
myImage.isHidden = !myImage.isHidden
}
图像闪烁(使用图像视图alpha):-
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
Timer.scheduledTimer(timeInterval: 0.7, target: self, selector: #selector(self.alarmAlertActivate), userInfo: nil, repeats: true)
}
@objc func alarmAlertActivate(){
UIView.animate(withDuration: 0.7) {
self.alarmImage.alpha = self.alarmImage.alpha == 1.0 ? 0.0 : 1.0
}
}
答案 2 :(得分:1)
我想让我的按钮突然闪烁/闪烁,而不是像大多数 IOS 动画默认那样平滑地淡入淡出。上面接受的答案使用计时器来完成此操作,但您也可以设置常规动画师类来执行此操作 - 无需平滑。下面是一个使按钮边框颜色闪烁的示例,但这可以用于需要相同效果的任何内容。
这里的技巧是使用“关键帧”动画,并将计算模式设置为“离散”:
let color = CAKeyframeAnimation(keyPath: "borderColor")
color.values = [CGColor.white, CGColor.black, CGColor.white]
color.keyTimes = [0, 0.5, 1]
color.duration = 1
color.repeatCount = .infinity
color.calculationMode = .discrete
gotItButton.layer.borderWidth = 3
gotItButton.layer.add(color, forKey: "")