diff --git a/submodules/Display/Source/CAAnimationUtils.swift b/submodules/Display/Source/CAAnimationUtils.swift index d6cb20d55d..1466f46530 100644 --- a/submodules/Display/Source/CAAnimationUtils.swift +++ b/submodules/Display/Source/CAAnimationUtils.swift @@ -226,7 +226,7 @@ public extension CALayer { self.add(animationGroup, forKey: key) } - func animateKeyframes(values: [AnyObject], duration: Double, keyPath: String, timingFunction: String = CAMediaTimingFunctionName.linear.rawValue, mediaTimingFunction: CAMediaTimingFunction? = nil, removeOnCompletion: Bool = true, additive: Bool = false, completion: ((Bool) -> Void)? = nil) { + func animateKeyframes(values: [AnyObject], keyTimes: [NSNumber]? = nil, duration: Double, keyPath: String, timingFunction: String = CAMediaTimingFunctionName.linear.rawValue, mediaTimingFunction: CAMediaTimingFunction? = nil, removeOnCompletion: Bool = true, additive: Bool = false, completion: ((Bool) -> Void)? = nil) { let k = Float(UIView.animationDurationFactor()) var speed: Float = 1.0 if k != 0 && k != 1 { @@ -235,17 +235,21 @@ public extension CALayer { let animation = CAKeyframeAnimation(keyPath: keyPath) animation.values = values - var keyTimes: [NSNumber] = [] - for i in 0 ..< values.count { - if i == 0 { - keyTimes.append(0.0) - } else if i == values.count - 1 { - keyTimes.append(1.0) - } else { - keyTimes.append((Double(i) / Double(values.count - 1)) as NSNumber) + var effectiveKeyTimes: [NSNumber] = [] + if let keyTimes { + effectiveKeyTimes = keyTimes + } else { + for i in 0 ..< values.count { + if i == 0 { + effectiveKeyTimes.append(0.0) + } else if i == values.count - 1 { + effectiveKeyTimes.append(1.0) + } else { + effectiveKeyTimes.append((Double(i) / Double(values.count - 1)) as NSNumber) + } } } - animation.keyTimes = keyTimes + animation.keyTimes = effectiveKeyTimes animation.speed = speed animation.duration = duration animation.isAdditive = additive diff --git a/submodules/DrawingUI/Sources/DrawingEntitiesView.swift b/submodules/DrawingUI/Sources/DrawingEntitiesView.swift index 0c6f7f3736..412bf86c0c 100644 --- a/submodules/DrawingUI/Sources/DrawingEntitiesView.swift +++ b/submodules/DrawingUI/Sources/DrawingEntitiesView.swift @@ -617,10 +617,11 @@ public final class DrawingEntitiesView: UIView, TGPhotoDrawingEntitiesView { return intersectedViews.last } - public func selectEntity(_ entity: DrawingEntity?) { + public func selectEntity(_ entity: DrawingEntity?, animate: Bool = true) { if entity?.isMedia == true { return } + var selectionChanged = false if entity !== self.selectedEntityView?.entity { if let selectedEntityView = self.selectedEntityView { if let textEntityView = selectedEntityView as? DrawingTextEntityView, textEntityView.isEditing { @@ -634,9 +635,12 @@ public final class DrawingEntitiesView: UIView, TGPhotoDrawingEntitiesView { self.selectedEntityView = nil if let selectionView = selectedEntityView.selectionView { selectedEntityView.selectionView = nil - selectionView.removeFromSuperview() + selectionView.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.2, removeOnCompletion: false, completion: { [weak selectionView] _ in + selectionView?.removeFromSuperview() + }) } } + selectionChanged = true } if let entity = entity, let entityView = self.getView(for: entity.uuid) { @@ -653,6 +657,9 @@ public final class DrawingEntitiesView: UIView, TGPhotoDrawingEntitiesView { self.selectionContainerView?.addSubview(selectionView) } entityView.update() + if selectionChanged && animate { + entityView.animateSelection() + } } self.selectionChanged(self.selectedEntityView?.entity) @@ -725,6 +732,7 @@ public final class DrawingEntitiesView: UIView, TGPhotoDrawingEntitiesView { selectedEntityView.layer.animateAlpha(from: 1.0, to: 0.0, duration: 0.2, removeOnCompletion: false, completion: { _ in self.remove(uuid: selectedEntityView.entity.uuid) }) + selectedEntityView.selectionView?.removeFromSuperview() self.selectEntity(nil) Queue.mainQueue().after(0.3, { @@ -776,7 +784,7 @@ public final class DrawingEntitiesView: UIView, TGPhotoDrawingEntitiesView { } } else if gestureRecognizer.numberOfTouches == 1, let viewToSelect = self.entity(at: location) { - self.selectEntity(viewToSelect.entity) + self.selectEntity(viewToSelect.entity, animate: false) self.onInteractionUpdated(true) } else if gestureRecognizer.numberOfTouches == 2, let mediaEntityView = self.subviews.first(where: { $0 is DrawingEntityMediaView }) as? DrawingEntityMediaView { @@ -876,6 +884,31 @@ public class DrawingEntityView: UIView { return self.bounds } + func animateInsertion() { + self.layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.2) + + let values = [0.0, self.entity.scale * 1.1, self.entity.scale] + let keyTimes = [0.0, 0.67, 1.0] + self.layer.animateKeyframes(values: values as [NSNumber], keyTimes: keyTimes as [NSNumber], duration: 0.35, keyPath: "transform.scale") + + if let selectionView = self.selectionView { + selectionView.layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.2, delay: 0.3) + } + } + + func animateSelection() { + guard let selectionView = self.selectionView else { + return + } + + selectionView.layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.2, delay: 0.1) + selectionView.layer.animateScale(from: 0.87, to: 1.0, duration: 0.2, delay: 0.1) + + let values = [self.entity.scale, self.entity.scale * 0.88, self.entity.scale] + let keyTimes = [0.0, 0.33, 1.0] + self.layer.animateKeyframes(values: values as [NSNumber], keyTimes: keyTimes as [NSNumber], duration: 0.3, keyPath: "transform.scale") + } + public func play() { } diff --git a/submodules/DrawingUI/Sources/DrawingScreen.swift b/submodules/DrawingUI/Sources/DrawingScreen.swift index 9e8944c062..c32ad93b51 100644 --- a/submodules/DrawingUI/Sources/DrawingScreen.swift +++ b/submodules/DrawingUI/Sources/DrawingScreen.swift @@ -3122,7 +3122,7 @@ public final class DrawingToolsInteraction { entity.scale = scale } self.entitiesView.add(entity) - self.entitiesView.selectEntity(entity) + self.entitiesView.selectEntity(entity, animate: !(entity is DrawingTextEntity)) if let entityView = self.entitiesView.getView(for: entity.uuid) { if let textEntityView = entityView as? DrawingTextEntityView { @@ -3138,12 +3138,7 @@ public final class DrawingToolsInteraction { entityView.seek(to: 0.0) } - entityView.layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.2) - entityView.layer.animateScale(from: 0.1, to: entity.scale, duration: 0.2) - - if let selectionView = entityView.selectionView { - selectionView.layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.2, delay: 0.2) - } + entityView.animateInsertion() } } } diff --git a/submodules/DrawingUI/Sources/DrawingTextEntity.swift b/submodules/DrawingUI/Sources/DrawingTextEntity.swift index fca26f09cd..6bd096d1ca 100644 --- a/submodules/DrawingUI/Sources/DrawingTextEntity.swift +++ b/submodules/DrawingUI/Sources/DrawingTextEntity.swift @@ -82,6 +82,10 @@ public final class DrawingTextEntityView: DrawingEntityView, UITextViewDelegate self.displayLink?.invalidate() } + override func animateInsertion() { + + } + private var isSuspended = false private var _isEditing = false public var isEditing: Bool { diff --git a/submodules/DrawingUI/Sources/DrawingVectorEntity.swift b/submodules/DrawingUI/Sources/DrawingVectorEntity.swift index 87a7b99a79..bc5415c2b3 100644 --- a/submodules/DrawingUI/Sources/DrawingVectorEntity.swift +++ b/submodules/DrawingUI/Sources/DrawingVectorEntity.swift @@ -29,6 +29,14 @@ final class DrawingVectorEntityView: DrawingEntityView { return max(10.0, max(self.vectorEntity.referenceDrawingSize.width, self.vectorEntity.referenceDrawingSize.height) * 0.1) } + override func animateSelection() { + guard let selectionView = self.selectionView else { + return + } + + selectionView.layer.animateAlpha(from: 0.0, to: 1.0, duration: 0.2, delay: 0.1) + } + override func update(animated: Bool) { self.center = CGPoint(x: self.vectorEntity.drawingSize.width * 0.5, y: self.vectorEntity.drawingSize.height * 0.5) self.bounds = CGRect(origin: .zero, size: self.vectorEntity.drawingSize) diff --git a/submodules/TelegramUI/Components/MediaEditorScreen/Sources/MediaEditorScreen.swift b/submodules/TelegramUI/Components/MediaEditorScreen/Sources/MediaEditorScreen.swift index f0fa3733e2..0a77d4ecc8 100644 --- a/submodules/TelegramUI/Components/MediaEditorScreen/Sources/MediaEditorScreen.swift +++ b/submodules/TelegramUI/Components/MediaEditorScreen/Sources/MediaEditorScreen.swift @@ -2901,7 +2901,7 @@ public final class MediaEditorScreen: ViewController, UIDropInteractionDelegate if let self { if let content { let stickerEntity = DrawingStickerEntity(content: content) - self.interaction?.insertEntity(stickerEntity) + self.interaction?.insertEntity(stickerEntity, scale: 1.33) self.hasAnyChanges = true self.controller?.isSavingAvailable = true