use*_*930 6 uiimageview uiviewanimation ios swift
我有一个UIImageView图像,这个图像是一个典型的地图选择器.我把它放在故事板中的地图上并分配了一些约束.
当用户拖动我的地图时,该图像保持不变,我想找到一种动画方式 - 基本上我想要稍微挤压它以使其看起来像被拖动.
我已经有两种方法:
func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
}
Run Code Online (Sandbox Code Playgroud)
和
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何动画更改为UIImageView.
我尝试添加regionWillChangeAnimated以下代码:
myPositionPicker.animate(withDuration: 1.5, delay: 0.05 * Double(index), usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
yawpPositionPicker.transform = CGAffineTransform(translationX: 0, y: 0)
}, completion: nil)
Run Code Online (Sandbox Code Playgroud)
(myPositionPicker我在哪里UIImageView),但我收到了错误
Static member animate cannot be used on instance of type UIImageView
Run Code Online (Sandbox Code Playgroud)
我认为对我来说最好的动画是,UIViewAnimationOptions.curveEaseIn但我不知道如何将它附加到我的UIImageView.你能给我一些提示吗?
首先,您尝试在实例myPositionPicker上使用静态方法“animate” 。这是错误的。只需更换
myPositionPicker.animate(withDuration: 1.5, delay: 0.05 * Double(index), usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
yawpPositionPicker.transform = CGAffineTransform(translationX: 0, y: 0)
}, completion: nil)
Run Code Online (Sandbox Code Playgroud)
和:
UIView.animate(withDuration: 1.5, delay: 0.05 * Double(index), usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
// Do whatever animations you want here.
}, completion: nil)
Run Code Online (Sandbox Code Playgroud)
接下来,如果您想创建一些简单的动画(例如更改 UIImageView 位置或更改其大小) - 您可以只使用已经创建的约束,而不使用仿射变换。
例如,如果您的图像视图与地图视图垂直居中。首先,在 UIViewController 中为此约束创建一个特殊的 @IBOutlet:
@IBOutlet var pickerCenterYConsraint: NSLayoutConstraint!
Run Code Online (Sandbox Code Playgroud)
其次,将此出口连接到故事板中的约束。
第三,据我了解,您的视图控制器是地图视图的代表。因此,让我们像这样实现地图视图委托协议:
func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
UIView.animate(withDuration: 1.5, delay: 0.05, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
self.pickerCenterYConsraint.constant = 25.0
self.view.layoutIfNeeded()
}, completion: nil)
}
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
UIView.animate(withDuration: 1.5, delay: 0.05, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
self.pickerCenterYConsraint.constant = 0.0
self.view.layoutIfNeeded()
}, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
就这样!现在,您的图像视图将在地图视图拖动期间动画化。如果您不喜欢这个解决方案 - 您可以根据约束创建自己的动画。基本上你可以在有限制的情况下做很多事情。例如,除了旋转之外。