Skip to content

Commit

Permalink
Merge pull request #12 from mono0926/enhance-animatable-path
Browse files Browse the repository at this point in the history
Enhance animatable path
  • Loading branch information
mono0926 committed May 21, 2017
2 parents f7937ac + eb4fdf6 commit 57ec95a
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 50 deletions.
12 changes: 12 additions & 0 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,15 @@ class ViewController: UIViewController {
}
}

class AnimatableDoneView2: UIView, HasAnimatablePath {
let animatableLayer = CAShapeLayer()
var animatablePath: UIBezierPath {
let length = frame.width
let path = UIBezierPath()
path.move(to: CGPoint(x: length * 0.196, y: length * 0.527))
path.addLine(to: CGPoint(x: length * 0.47, y: length * 0.777))
path.addLine(to: CGPoint(x: length * 0.99, y: length * 0.25))
return path
}
}

12 changes: 4 additions & 8 deletions NativePopup.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@

/* Begin PBXBuildFile section */
FE4CECF61ED1171400D5DB31 /* AnimatableCrossView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE4CECF51ED1171400D5DB31 /* AnimatableCrossView.swift */; };
FE4CECF81ED119F800D5DB31 /* HasAnimatablePath.swift in Sources */ = {isa = PBXBuildFile; fileRef = FE4CECF71ED119F800D5DB31 /* HasAnimatablePath.swift */; };
FE8533901EAC7C92004BB756 /* NativePopup.h in Headers */ = {isa = PBXBuildFile; fileRef = FE85338E1EAC7C92004BB756 /* NativePopup.h */; settings = {ATTRIBUTES = (Public, ); }; };
FEB0316D1ECFF6770039A1C6 /* Preset.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEB0316C1ECFF6770039A1C6 /* Preset.swift */; };
FEB031711ECFF6A30039A1C6 /* Bundle.extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEB031701ECFF6A30039A1C6 /* Bundle.extension.swift */; };
FEB031731ECFF6E00039A1C6 /* InitialEffectType.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEB031721ECFF6E00039A1C6 /* InitialEffectType.swift */; };
FEB031751ECFFB6C0039A1C6 /* Animatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEB031741ECFFB6C0039A1C6 /* Animatable.swift */; };
FEB031771ECFFE120039A1C6 /* AnimatableDoneView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEB031761ECFFE120039A1C6 /* AnimatableDoneView.swift */; };
FEB031791ED014D70039A1C6 /* HasAnimatableLayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEB031781ED014D70039A1C6 /* HasAnimatableLayer.swift */; };
FEB031791ED014D70039A1C6 /* HasAnimatablePath.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEB031781ED014D70039A1C6 /* HasAnimatablePath.swift */; };
FECA87BA1EAC7CCF00D07CB1 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FECA87B91EAC7CCF00D07CB1 /* AppDelegate.swift */; };
FECA87BC1EAC7CCF00D07CB1 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FECA87BB1EAC7CCF00D07CB1 /* ViewController.swift */; };
FECA87BF1EAC7CCF00D07CB1 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FECA87BD1EAC7CCF00D07CB1 /* Main.storyboard */; };
Expand Down Expand Up @@ -57,7 +56,6 @@

/* Begin PBXFileReference section */
FE4CECF51ED1171400D5DB31 /* AnimatableCrossView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnimatableCrossView.swift; sourceTree = "<group>"; };
FE4CECF71ED119F800D5DB31 /* HasAnimatablePath.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HasAnimatablePath.swift; sourceTree = "<group>"; };
FE85338B1EAC7C92004BB756 /* NativePopup.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = NativePopup.framework; sourceTree = BUILT_PRODUCTS_DIR; };
FE85338E1EAC7C92004BB756 /* NativePopup.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NativePopup.h; sourceTree = "<group>"; };
FE85338F1EAC7C92004BB756 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand All @@ -66,7 +64,7 @@
FEB031721ECFF6E00039A1C6 /* InitialEffectType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InitialEffectType.swift; sourceTree = "<group>"; };
FEB031741ECFFB6C0039A1C6 /* Animatable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Animatable.swift; sourceTree = "<group>"; };
FEB031761ECFFE120039A1C6 /* AnimatableDoneView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnimatableDoneView.swift; sourceTree = "<group>"; };
FEB031781ED014D70039A1C6 /* HasAnimatableLayer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HasAnimatableLayer.swift; sourceTree = "<group>"; };
FEB031781ED014D70039A1C6 /* HasAnimatablePath.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HasAnimatablePath.swift; sourceTree = "<group>"; };
FECA87B71EAC7CCF00D07CB1 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
FECA87B91EAC7CCF00D07CB1 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
FECA87BB1EAC7CCF00D07CB1 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -105,8 +103,7 @@
isa = PBXGroup;
children = (
FEB031741ECFFB6C0039A1C6 /* Animatable.swift */,
FEB031781ED014D70039A1C6 /* HasAnimatableLayer.swift */,
FE4CECF71ED119F800D5DB31 /* HasAnimatablePath.swift */,
FEB031781ED014D70039A1C6 /* HasAnimatablePath.swift */,
FE4CECF51ED1171400D5DB31 /* AnimatableCrossView.swift */,
FEB031761ECFFE120039A1C6 /* AnimatableDoneView.swift */,
);
Expand Down Expand Up @@ -293,10 +290,9 @@
FEB031711ECFF6A30039A1C6 /* Bundle.extension.swift in Sources */,
FEB031731ECFF6E00039A1C6 /* InitialEffectType.swift in Sources */,
FEB031771ECFFE120039A1C6 /* AnimatableDoneView.swift in Sources */,
FEB031791ED014D70039A1C6 /* HasAnimatableLayer.swift in Sources */,
FEB031791ED014D70039A1C6 /* HasAnimatablePath.swift in Sources */,
FEB031751ECFFB6C0039A1C6 /* Animatable.swift in Sources */,
FECA87E31EACC7F000D07CB1 /* UIImageConvertible.swift in Sources */,
FE4CECF81ED119F800D5DB31 /* HasAnimatablePath.swift in Sources */,
FECA87F21EADBA3C00D07CB1 /* Extension.swift in Sources */,
FECA87D81EAC7D9E00D07CB1 /* NativePopup.swift in Sources */,
FEB0316D1ECFF6770039A1C6 /* Preset.swift in Sources */,
Expand Down
5 changes: 2 additions & 3 deletions NativePopup/Animatable/Animatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

import Foundation

protocol Animatable {
public protocol Animatable {
func animate()
var duration: TimeInterval { get }
}

extension Animatable {
public extension Animatable {
var duration: TimeInterval { return 0.3 }
}
5 changes: 3 additions & 2 deletions NativePopup/Animatable/AnimatableCrossView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

import Foundation

class AnimatableCrossView: AnimatablePathView {
class AnimatableCrossView: UIView, HasAnimatablePath {
let animatableLayer = CAShapeLayer()
var duration: TimeInterval { return 0.4 }
override var animatablePath: UIBezierPath {
var animatablePath: UIBezierPath {
let length = frame.width
let path = UIBezierPath()
path.move(to: CGPoint(x: length * 0.1, y: length * 0.1))
Expand Down
5 changes: 3 additions & 2 deletions NativePopup/Animatable/AnimatableDoneView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

import Foundation

class AnimatableDoneView: AnimatablePathView {
override var animatablePath: UIBezierPath {
class AnimatableDoneView: UIView, HasAnimatablePath {
let animatableLayer = CAShapeLayer()
var animatablePath: UIBezierPath {
let length = frame.width
let path = UIBezierPath()
path.move(to: CGPoint(x: length * 0.196, y: length * 0.527))
Expand Down
25 changes: 0 additions & 25 deletions NativePopup/Animatable/HasAnimatableLayer.swift

This file was deleted.

30 changes: 22 additions & 8 deletions NativePopup/Animatable/HasAnimatablePath.swift
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
//
// HasAnimatablePath.swift
// HasAnimatableLayer.swift
// NativePopup
//
// Created by mono on 2017/05/21.
// Created by mono on 2017/05/20.
// Copyright © 2017 mono. All rights reserved.
//

import Foundation

open class AnimatablePathView: UIView, HasAnimatableLayer {
let animatableLayer = CAShapeLayer()
open var animatablePath: UIBezierPath { fatalError("Should be overridden.") }
func setupLayer() {
public protocol HasAnimatablePath: Animatable {
var layer: CALayer { get }
var tintColor: UIColor! { get }
/** Should return same instance */
var animatableLayer: CAShapeLayer { get }
var animatablePath: UIBezierPath { get }
}

public extension HasAnimatablePath {
public func animate() {
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.duration = duration
animation.fromValue = 0
animation.toValue = 1
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
animatableLayer.strokeEnd = 1
animatableLayer.add(animation, forKey: "animation")
}
public func configureAnimatableLayer() {
animatableLayer.path = animatablePath.cgPath
animatableLayer.fillColor = UIColor.clear.cgColor
animatableLayer.strokeColor = tintColor.cgColor
animatableLayer.strokeColor = tintColor?.cgColor
animatableLayer.lineWidth = 9
animatableLayer.lineCap = kCALineCapRound
animatableLayer.lineJoin = kCALineCapRound
animatableLayer.strokeEnd = 0
layer.addSublayer(animatableLayer)
}
}
5 changes: 3 additions & 2 deletions NativePopup/NativePopup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ public class NativePopup: UIView {

[self, effectView, imageView, titleLabel, messageLabel].forEach { $0.translatesAutoresizingMaskIntoConstraints = false }

if let animatable = imageView as? AnimatablePathView {
if let animatable = imageView as? HasAnimatablePath {
imageView.layoutIfNeeded()
animatable.setupLayer()
animatable.configureAnimatableLayer()
animatable.layer.addSublayer(animatable.animatableLayer)
}
}

Expand Down

0 comments on commit 57ec95a

Please sign in to comment.