Ответы пользователя по тегу Анимация
  • Как сделать анимацию для UIImageView?

    Tehdrew
    @Tehdrew
    iOS Developer
    1.
    Scale изображения

    imageView.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
    
    UIView.animate(withDuration: 0.6,
        animations: { [weak self] in
            self?.imageView.transform = .identity
        })


    Появление изображения
    imageView.alpha = 0.0
    
    UIView.animate(withDuration: 0.6,
        animations: { [weak self] in
            self?.imageView.alpha = 1.0
        })


    2. Функция для вращения UIImageView, Outlet будет в данном примере называться 'imageBackground'.
    private let animationRepeatDuration: CFTimeInterval = 2.0
    private let rotationAnimatonKey: String = "rotationAnimation"
    
    func animate(completion: @escaping () -> Void) {
            
            CATransaction.begin()
            
            CATransaction.setCompletionBlock { [weak self] in
                
                guard
                    let rotationAnimatonKey = self?.rotationAnimatonKey,
                    self?.imageBackground.layer.animation(forKey: rotationAnimatonKey) != nil
                    else {
                    completion()
                    return
                }
            }
            
            let rotation: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.y")
            rotation.toValue = NSNumber(value: Double.pi * 2)
            rotation.repeatCount = Float.greatestFiniteMagnitude
            rotation.repeatDuration = animationRepeatDuration
            imageBackground.layer.add(rotation, forKey: rotationAnimatonKey)
            
            CATransaction.commit()
    }
    Ответ написан
    Комментировать