Various improvements

This commit is contained in:
Ilya Laktyushin 2023-07-16 03:37:43 +02:00
parent b263f37d73
commit f801c81a9d
6 changed files with 65 additions and 21 deletions

View File

@ -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

View File

@ -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() {
}

View File

@ -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()
}
}
}

View File

@ -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 {

View File

@ -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)

View File

@ -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