diff --git a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO.xcodeproj/project.pbxproj b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO.xcodeproj/project.pbxproj index 870b57b..1921900 100644 --- a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO.xcodeproj/project.pbxproj +++ b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO.xcodeproj/project.pbxproj @@ -14,6 +14,8 @@ 4FBAA7081B4D807F00C8657F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4FBAA7061B4D807F00C8657F /* Main.storyboard */; }; 4FBAA70A1B4D807F00C8657F /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4FBAA7091B4D807F00C8657F /* Images.xcassets */; }; 4FBAA70D1B4D807F00C8657F /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4FBAA70B1B4D807F00C8657F /* LaunchScreen.xib */; }; + 98811D731E38C3C3003F7B12 /* DOFavoriteButton.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4F60711B1B6366ED0058F570 /* DOFavoriteButton.framework */; }; + 98811D741E38C3C3003F7B12 /* DOFavoriteButton.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 4F60711B1B6366ED0058F570 /* DOFavoriteButton.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; /* End PBXBuildFile section */ /* Begin PBXBuildRule section */ @@ -28,6 +30,30 @@ }; /* End PBXBuildRule section */ +/* Begin PBXContainerItemProxy section */ + 98811D751E38C3C3003F7B12 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 4FBAA6F51B4D807F00C8657F /* Project object */; + proxyType = 1; + remoteGlobalIDString = 4F60711A1B6366ED0058F570; + remoteInfo = DOFavoriteButton; + }; +/* End PBXContainerItemProxy section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 98811D771E38C3C3003F7B12 /* Embed Frameworks */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = ""; + dstSubfolderSpec = 10; + files = ( + 98811D741E38C3C3003F7B12 /* DOFavoriteButton.framework in Embed Frameworks */, + ); + name = "Embed Frameworks"; + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXCopyFilesBuildPhase section */ + /* Begin PBXFileReference section */ 4F60711B1B6366ED0058F570 /* DOFavoriteButton.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = DOFavoriteButton.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 4F60711E1B6366ED0058F570 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -54,6 +80,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 98811D731E38C3C3003F7B12 /* DOFavoriteButton.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -157,10 +184,12 @@ 4FBAA6F91B4D807F00C8657F /* Sources */, 4FBAA6FA1B4D807F00C8657F /* Frameworks */, 4FBAA6FB1B4D807F00C8657F /* Resources */, + 98811D771E38C3C3003F7B12 /* Embed Frameworks */, ); buildRules = ( ); dependencies = ( + 98811D761E38C3C3003F7B12 /* PBXTargetDependency */, ); name = "DOFavoriteButton-DEMO"; productName = "DOFavoriteButton-DEMO"; @@ -183,6 +212,7 @@ }; 4FBAA6FC1B4D807F00C8657F = { CreatedOnToolsVersion = 6.4; + LastSwiftMigration = 0810; }; }; }; @@ -245,6 +275,14 @@ }; /* End PBXSourcesBuildPhase section */ +/* Begin PBXTargetDependency section */ + 98811D761E38C3C3003F7B12 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 4F60711A1B6366ED0058F570 /* DOFavoriteButton */; + targetProxy = 98811D751E38C3C3003F7B12 /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + /* Begin PBXVariantGroup section */ 4FBAA7061B4D807F00C8657F /* Main.storyboard */ = { isa = PBXVariantGroup; @@ -286,6 +324,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0.1; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -307,6 +346,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "co.devlog.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0.1; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -399,24 +439,28 @@ 4FBAA71D1B4D807F00C8657F /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; INFOPLIST_FILE = "DOFavoriteButton-DEMO/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "co.devlog.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0.1; }; name = Debug; }; 4FBAA71E1B4D807F00C8657F /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; INFOPLIST_FILE = "DOFavoriteButton-DEMO/Info.plist"; IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "co.devlog.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0.1; }; name = Release; }; diff --git a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/AppDelegate.swift b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/AppDelegate.swift index d3cf69d..5ce43bf 100644 --- a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/AppDelegate.swift +++ b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/AppDelegate.swift @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - func applicationDidEnterBackground(application: UIApplication) { + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - func applicationWillEnterForeground(application: UIApplication) { + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - func applicationDidBecomeActive(application: UIApplication) { + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } diff --git a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/ViewController.swift b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/ViewController.swift index 99be5ed..600bd84 100644 --- a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/ViewController.swift +++ b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/ViewController.swift @@ -10,7 +10,7 @@ import UIKit import DOFavoriteButton class ViewController: UIViewController { - + @IBOutlet var heartButton: DOFavoriteButton! override func viewDidLoad() { super.viewDidLoad() @@ -21,47 +21,47 @@ class ViewController: UIViewController { let y = self.view.frame.height / 2 - 22 // star button - let starButton = DOFavoriteButton(frame: CGRectMake(x, y, 44, 44), image: UIImage(named: "star")) - starButton.addTarget(self, action: Selector("tappedButton:"), forControlEvents: UIControlEvents.TouchUpInside) + let starButton = DOFavoriteButton(frame: CGRect(x: x, y: y, width: 44, height: 44), image: UIImage(named: "star")) + starButton.addTarget(self, action: #selector(ViewController.tappedButton), for: UIControlEvents.touchUpInside) self.view.addSubview(starButton) x += width // heart button - let heartButton = DOFavoriteButton(frame: CGRectMake(x, y, 44, 44), image: UIImage(named: "heart")) + let heartButton = DOFavoriteButton(frame: CGRect(x: x, y: y, width: 44, height: 44), image: UIImage(named: "heart")) heartButton.imageColorOn = UIColor(red: 254/255, green: 110/255, blue: 111/255, alpha: 1.0) heartButton.circleColor = UIColor(red: 254/255, green: 110/255, blue: 111/255, alpha: 1.0) heartButton.lineColor = UIColor(red: 226/255, green: 96/255, blue: 96/255, alpha: 1.0) - heartButton.addTarget(self, action: Selector("tappedButton:"), forControlEvents: UIControlEvents.TouchUpInside) + heartButton.addTarget(self, action: #selector(ViewController.tappedButton), for: UIControlEvents.touchUpInside) self.view.addSubview(heartButton) x += width // like button - let likeButton = DOFavoriteButton(frame: CGRectMake(x, y, 44, 44), image: UIImage(named: "like")) + let likeButton = DOFavoriteButton(frame: CGRect(x: x, y: y, width: 44, height: 44), image: UIImage(named: "like")) likeButton.imageColorOn = UIColor(red: 52/255, green: 152/255, blue: 219/255, alpha: 1.0) likeButton.circleColor = UIColor(red: 52/255, green: 152/255, blue: 219/255, alpha: 1.0) likeButton.lineColor = UIColor(red: 41/255, green: 128/255, blue: 185/255, alpha: 1.0) - likeButton.addTarget(self, action: Selector("tappedButton:"), forControlEvents: UIControlEvents.TouchUpInside) + likeButton.addTarget(self, action: #selector(ViewController.tappedButton), for: UIControlEvents.touchUpInside) self.view.addSubview(likeButton) x += width // smile button - let smileButton = DOFavoriteButton(frame: CGRectMake(x, y, 44, 44), image: UIImage(named: "smile")) + let smileButton = DOFavoriteButton(frame: CGRect(x: x, y: y, width: 44, height: 44), image: UIImage(named: "smile")) smileButton.imageColorOn = UIColor(red: 45/255, green: 204/255, blue: 112/255, alpha: 1.0) smileButton.circleColor = UIColor(red: 45/255, green: 204/255, blue: 112/255, alpha: 1.0) smileButton.lineColor = UIColor(red: 45/255, green: 195/255, blue: 106/255, alpha: 1.0) - smileButton.addTarget(self, action: Selector("tappedButton:"), forControlEvents: UIControlEvents.TouchUpInside) + smileButton.addTarget(self, action: #selector(ViewController.tappedButton), for: UIControlEvents.touchUpInside) self.view.addSubview(smileButton) - self.heartButton.addTarget(self, action: Selector("tappedButton:"), forControlEvents: UIControlEvents.TouchUpInside) + self.heartButton.addTarget(self, action: #selector(ViewController.tappedButton), for: UIControlEvents.touchUpInside) } - + override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } - + func tappedButton(sender: DOFavoriteButton) { - if sender.selected { + if sender.isSelected { sender.deselect() } else { sender.select() diff --git a/DOFavoriteButton/DOFavoriteButton.swift b/DOFavoriteButton/DOFavoriteButton.swift index 4b7e9b8..b549065 100644 --- a/DOFavoriteButton/DOFavoriteButton.swift +++ b/DOFavoriteButton/DOFavoriteButton.swift @@ -12,54 +12,54 @@ import UIKit @IBDesignable -public class DOFavoriteButton: UIButton { - - private var imageShape: CAShapeLayer! - @IBInspectable public var image: UIImage! { +open class DOFavoriteButton: UIButton { + + fileprivate var imageShape: CAShapeLayer! + @IBInspectable open var image: UIImage! { didSet { createLayers(image: image) } } - @IBInspectable public var imageColorOn: UIColor! = UIColor(red: 255/255, green: 172/255, blue: 51/255, alpha: 1.0) { + @IBInspectable open var imageColorOn: UIColor! = UIColor(red: 255/255, green: 172/255, blue: 51/255, alpha: 1.0) { didSet { - if (selected) { - imageShape.fillColor = imageColorOn.CGColor + if (isSelected) { + imageShape.fillColor = imageColorOn.cgColor } } } - @IBInspectable public var imageColorOff: UIColor! = UIColor(red: 136/255, green: 153/255, blue: 166/255, alpha: 1.0) { + @IBInspectable open var imageColorOff: UIColor! = UIColor(red: 136/255, green: 153/255, blue: 166/255, alpha: 1.0) { didSet { - if (!selected) { - imageShape.fillColor = imageColorOff.CGColor + if (!isSelected) { + imageShape.fillColor = imageColorOff.cgColor } } } - - private var circleShape: CAShapeLayer! - private var circleMask: CAShapeLayer! - @IBInspectable public var circleColor: UIColor! = UIColor(red: 255/255, green: 172/255, blue: 51/255, alpha: 1.0) { + + fileprivate var circleShape: CAShapeLayer! + fileprivate var circleMask: CAShapeLayer! + @IBInspectable open var circleColor: UIColor! = UIColor(red: 255/255, green: 172/255, blue: 51/255, alpha: 1.0) { didSet { - circleShape.fillColor = circleColor.CGColor + circleShape.fillColor = circleColor.cgColor } } - - private var lines: [CAShapeLayer]! - @IBInspectable public var lineColor: UIColor! = UIColor(red: 250/255, green: 120/255, blue: 68/255, alpha: 1.0) { + + fileprivate var lines: [CAShapeLayer]! + @IBInspectable open var lineColor: UIColor! = UIColor(red: 250/255, green: 120/255, blue: 68/255, alpha: 1.0) { didSet { for line in lines { - line.strokeColor = lineColor.CGColor + line.strokeColor = lineColor.cgColor } } } - - private let circleTransform = CAKeyframeAnimation(keyPath: "transform") - private let circleMaskTransform = CAKeyframeAnimation(keyPath: "transform") - private let lineStrokeStart = CAKeyframeAnimation(keyPath: "strokeStart") - private let lineStrokeEnd = CAKeyframeAnimation(keyPath: "strokeEnd") - private let lineOpacity = CAKeyframeAnimation(keyPath: "opacity") - private let imageTransform = CAKeyframeAnimation(keyPath: "transform") - - @IBInspectable public var duration: Double = 1.0 { + + fileprivate let circleTransform = CAKeyframeAnimation(keyPath: "transform") + fileprivate let circleMaskTransform = CAKeyframeAnimation(keyPath: "transform") + fileprivate let lineStrokeStart = CAKeyframeAnimation(keyPath: "strokeStart") + fileprivate let lineStrokeEnd = CAKeyframeAnimation(keyPath: "strokeEnd") + fileprivate let lineOpacity = CAKeyframeAnimation(keyPath: "opacity") + fileprivate let imageTransform = CAKeyframeAnimation(keyPath: "transform") + + @IBInspectable open var duration: Double = 1.0 { didSet { circleTransform.duration = 0.333 * duration // 0.0333 * 10 circleMaskTransform.duration = 0.333 * duration // 0.0333 * 10 @@ -69,68 +69,68 @@ public class DOFavoriteButton: UIButton { imageTransform.duration = 1.0 * duration //0.0333 * 30 } } - - override public var selected : Bool { + + override open var isSelected : Bool { didSet { - if (selected != oldValue) { - if selected { - imageShape.fillColor = imageColorOn.CGColor + if (isSelected != oldValue) { + if isSelected { + imageShape.fillColor = imageColorOn.cgColor } else { deselect() } } } } - + public convenience init() { - self.init(frame: CGRectZero) + self.init(frame: CGRect.zero) } - + public override convenience init(frame: CGRect) { self.init(frame: frame, image: UIImage()) } - + public init(frame: CGRect, image: UIImage!) { super.init(frame: frame) self.image = image createLayers(image: image) addTargets() } - + public required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! createLayers(image: UIImage()) addTargets() } - - private func createLayers(image image: UIImage!) { + + fileprivate func createLayers(image: UIImage!) { self.layer.sublayers = nil - - let imageFrame = CGRectMake(frame.size.width / 2 - frame.size.width / 4, frame.size.height / 2 - frame.size.height / 4, frame.size.width / 2, frame.size.height / 2) - let imgCenterPoint = CGPointMake(CGRectGetMidX(imageFrame), CGRectGetMidY(imageFrame)) - let lineFrame = CGRectMake(imageFrame.origin.x - imageFrame.width / 4, imageFrame.origin.y - imageFrame.height / 4 , imageFrame.width * 1.5, imageFrame.height * 1.5) - + + let imageFrame = CGRect(x: frame.size.width / 2 - frame.size.width / 4, y: frame.size.height / 2 - frame.size.height / 4, width: frame.size.width / 2, height: frame.size.height / 2) + let imgCenterPoint = CGPoint(x: imageFrame.midX, y: imageFrame.midY) + let lineFrame = CGRect(x: imageFrame.origin.x - imageFrame.width / 4, y: imageFrame.origin.y - imageFrame.height / 4 , width: imageFrame.width * 1.5, height: imageFrame.height * 1.5) + //=============== // circle layer //=============== circleShape = CAShapeLayer() circleShape.bounds = imageFrame circleShape.position = imgCenterPoint - circleShape.path = UIBezierPath(ovalInRect: imageFrame).CGPath - circleShape.fillColor = circleColor.CGColor + circleShape.path = UIBezierPath(ovalIn: imageFrame).cgPath + circleShape.fillColor = circleColor.cgColor circleShape.transform = CATransform3DMakeScale(0.0, 0.0, 1.0) self.layer.addSublayer(circleShape) - + circleMask = CAShapeLayer() circleMask.bounds = imageFrame circleMask.position = imgCenterPoint circleMask.fillRule = kCAFillRuleEvenOdd circleShape.mask = circleMask - + let maskPath = UIBezierPath(rect: imageFrame) - maskPath.addArcWithCenter(imgCenterPoint, radius: 0.1, startAngle: CGFloat(0.0), endAngle: CGFloat(M_PI * 2), clockwise: true) - circleMask.path = maskPath.CGPath - + maskPath.addArc(withCenter: imgCenterPoint, radius: 0.1, startAngle: CGFloat(0.0), endAngle: CGFloat(M_PI * 2), clockwise: true) + circleMask.path = maskPath.cgPath + //=============== // line layer //=============== @@ -141,15 +141,17 @@ public class DOFavoriteButton: UIButton { line.position = imgCenterPoint line.masksToBounds = true line.actions = ["strokeStart": NSNull(), "strokeEnd": NSNull()] - line.strokeColor = lineColor.CGColor + line.strokeColor = lineColor.cgColor line.lineWidth = 1.25 line.miterLimit = 1.25 line.path = { - let path = CGPathCreateMutable() - CGPathMoveToPoint(path, nil, CGRectGetMidX(lineFrame), CGRectGetMidY(lineFrame)) - CGPathAddLineToPoint(path, nil, lineFrame.origin.x + lineFrame.width / 2, lineFrame.origin.y) + let path = CGMutablePath() + path.move(to: CGPoint(x:lineFrame.midX, y:lineFrame.midY)) + path.addLine(to: CGPoint(x:lineFrame.origin.x + lineFrame.width / 2, y:lineFrame.origin.y)) + // CGPathMoveToPoint(path, nil, lineFrame.midX, lineFrame.midY) + // CGPathAddLineToPoint(path, nil, lineFrame.origin.x + lineFrame.width / 2, lineFrame.origin.y) return path - }() + }() line.lineCap = kCALineCapRound line.lineJoin = kCALineJoinRound line.strokeStart = 0.0 @@ -159,36 +161,36 @@ public class DOFavoriteButton: UIButton { self.layer.addSublayer(line) lines.append(line) } - + //=============== // image layer //=============== imageShape = CAShapeLayer() imageShape.bounds = imageFrame imageShape.position = imgCenterPoint - imageShape.path = UIBezierPath(rect: imageFrame).CGPath - imageShape.fillColor = imageColorOff.CGColor + imageShape.path = UIBezierPath(rect: imageFrame).cgPath + imageShape.fillColor = imageColorOff.cgColor imageShape.actions = ["fillColor": NSNull()] self.layer.addSublayer(imageShape) - + imageShape.mask = CALayer() - imageShape.mask!.contents = image.CGImage + imageShape.mask!.contents = image.cgImage imageShape.mask!.bounds = imageFrame imageShape.mask!.position = imgCenterPoint - + //============================== // circle transform animation //============================== circleTransform.duration = 0.333 // 0.0333 * 10 circleTransform.values = [ - NSValue(CATransform3D: CATransform3DMakeScale(0.0, 0.0, 1.0)), // 0/10 - NSValue(CATransform3D: CATransform3DMakeScale(0.5, 0.5, 1.0)), // 1/10 - NSValue(CATransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)), // 2/10 - NSValue(CATransform3D: CATransform3DMakeScale(1.2, 1.2, 1.0)), // 3/10 - NSValue(CATransform3D: CATransform3DMakeScale(1.3, 1.3, 1.0)), // 4/10 - NSValue(CATransform3D: CATransform3DMakeScale(1.37, 1.37, 1.0)), // 5/10 - NSValue(CATransform3D: CATransform3DMakeScale(1.4, 1.4, 1.0)), // 6/10 - NSValue(CATransform3D: CATransform3DMakeScale(1.4, 1.4, 1.0)) // 10/10 + NSValue(caTransform3D: CATransform3DMakeScale(0.0, 0.0, 1.0)), // 0/10 + NSValue(caTransform3D: CATransform3DMakeScale(0.5, 0.5, 1.0)), // 1/10 + NSValue(caTransform3D: CATransform3DMakeScale(1.0, 1.0, 1.0)), // 2/10 + NSValue(caTransform3D: CATransform3DMakeScale(1.2, 1.2, 1.0)), // 3/10 + NSValue(caTransform3D: CATransform3DMakeScale(1.3, 1.3, 1.0)), // 4/10 + NSValue(caTransform3D: CATransform3DMakeScale(1.37, 1.37, 1.0)), // 5/10 + NSValue(caTransform3D: CATransform3DMakeScale(1.4, 1.4, 1.0)), // 6/10 + NSValue(caTransform3D: CATransform3DMakeScale(1.4, 1.4, 1.0)) // 10/10 ] circleTransform.keyTimes = [ 0.0, // 0/10 @@ -200,18 +202,18 @@ public class DOFavoriteButton: UIButton { 0.6, // 6/10 1.0 // 10/10 ] - + circleMaskTransform.duration = 0.333 // 0.0333 * 10 circleMaskTransform.values = [ - NSValue(CATransform3D: CATransform3DIdentity), // 0/10 - NSValue(CATransform3D: CATransform3DIdentity), // 2/10 - NSValue(CATransform3D: CATransform3DMakeScale(imageFrame.width * 1.25, imageFrame.height * 1.25, 1.0)), // 3/10 - NSValue(CATransform3D: CATransform3DMakeScale(imageFrame.width * 2.688, imageFrame.height * 2.688, 1.0)), // 4/10 - NSValue(CATransform3D: CATransform3DMakeScale(imageFrame.width * 3.923, imageFrame.height * 3.923, 1.0)), // 5/10 - NSValue(CATransform3D: CATransform3DMakeScale(imageFrame.width * 4.375, imageFrame.height * 4.375, 1.0)), // 6/10 - NSValue(CATransform3D: CATransform3DMakeScale(imageFrame.width * 4.731, imageFrame.height * 4.731, 1.0)), // 7/10 - NSValue(CATransform3D: CATransform3DMakeScale(imageFrame.width * 5.0, imageFrame.height * 5.0, 1.0)), // 9/10 - NSValue(CATransform3D: CATransform3DMakeScale(imageFrame.width * 5.0, imageFrame.height * 5.0, 1.0)) // 10/10 + NSValue(caTransform3D: CATransform3DIdentity), // 0/10 + NSValue(caTransform3D: CATransform3DIdentity), // 2/10 + NSValue(caTransform3D: CATransform3DMakeScale(imageFrame.width * 1.25, imageFrame.height * 1.25, 1.0)), // 3/10 + NSValue(caTransform3D: CATransform3DMakeScale(imageFrame.width * 2.688, imageFrame.height * 2.688, 1.0)), // 4/10 + NSValue(caTransform3D: CATransform3DMakeScale(imageFrame.width * 3.923, imageFrame.height * 3.923, 1.0)), // 5/10 + NSValue(caTransform3D: CATransform3DMakeScale(imageFrame.width * 4.375, imageFrame.height * 4.375, 1.0)), // 6/10 + NSValue(caTransform3D: CATransform3DMakeScale(imageFrame.width * 4.731, imageFrame.height * 4.731, 1.0)), // 7/10 + NSValue(caTransform3D: CATransform3DMakeScale(imageFrame.width * 5.0, imageFrame.height * 5.0, 1.0)), // 9/10 + NSValue(caTransform3D: CATransform3DMakeScale(imageFrame.width * 5.0, imageFrame.height * 5.0, 1.0)) // 10/10 ] circleMaskTransform.keyTimes = [ 0.0, // 0/10 @@ -224,7 +226,7 @@ public class DOFavoriteButton: UIButton { 0.9, // 9/10 1.0 // 10/10 ] - + //============================== // line stroke animation //============================== @@ -255,7 +257,7 @@ public class DOFavoriteButton: UIButton { 0.944, // 17/18 1.0, // 18/18 ] - + lineStrokeEnd.duration = 0.6 //0.0333 * 18 lineStrokeEnd.values = [ 0.0, // 0/18 @@ -277,7 +279,7 @@ public class DOFavoriteButton: UIButton { 0.944, // 17/18 1.0, // 18/18 ] - + lineOpacity.duration = 1.0 //0.0333 * 30 lineOpacity.values = [ 1.0, // 0/30 @@ -289,29 +291,29 @@ public class DOFavoriteButton: UIButton { 0.4, // 12/30 0.567 // 17/30 ] - + //============================== // image transform animation //============================== imageTransform.duration = 1.0 //0.0333 * 30 imageTransform.values = [ - NSValue(CATransform3D: CATransform3DMakeScale(0.0, 0.0, 1.0)), // 0/30 - NSValue(CATransform3D: CATransform3DMakeScale(0.0, 0.0, 1.0)), // 3/30 - NSValue(CATransform3D: CATransform3DMakeScale(1.2, 1.2, 1.0)), // 9/30 - NSValue(CATransform3D: CATransform3DMakeScale(1.25, 1.25, 1.0)), // 10/30 - NSValue(CATransform3D: CATransform3DMakeScale(1.2, 1.2, 1.0)), // 11/30 - NSValue(CATransform3D: CATransform3DMakeScale(0.9, 0.9, 1.0)), // 14/30 - NSValue(CATransform3D: CATransform3DMakeScale(0.875, 0.875, 1.0)), // 15/30 - NSValue(CATransform3D: CATransform3DMakeScale(0.875, 0.875, 1.0)), // 16/30 - NSValue(CATransform3D: CATransform3DMakeScale(0.9, 0.9, 1.0)), // 17/30 - NSValue(CATransform3D: CATransform3DMakeScale(1.013, 1.013, 1.0)), // 20/30 - NSValue(CATransform3D: CATransform3DMakeScale(1.025, 1.025, 1.0)), // 21/30 - NSValue(CATransform3D: CATransform3DMakeScale(1.013, 1.013, 1.0)), // 22/30 - NSValue(CATransform3D: CATransform3DMakeScale(0.96, 0.96, 1.0)), // 25/30 - NSValue(CATransform3D: CATransform3DMakeScale(0.95, 0.95, 1.0)), // 26/30 - NSValue(CATransform3D: CATransform3DMakeScale(0.96, 0.96, 1.0)), // 27/30 - NSValue(CATransform3D: CATransform3DMakeScale(0.99, 0.99, 1.0)), // 29/30 - NSValue(CATransform3D: CATransform3DIdentity) // 30/30 + NSValue(caTransform3D: CATransform3DMakeScale(0.0, 0.0, 1.0)), // 0/30 + NSValue(caTransform3D: CATransform3DMakeScale(0.0, 0.0, 1.0)), // 3/30 + NSValue(caTransform3D: CATransform3DMakeScale(1.2, 1.2, 1.0)), // 9/30 + NSValue(caTransform3D: CATransform3DMakeScale(1.25, 1.25, 1.0)), // 10/30 + NSValue(caTransform3D: CATransform3DMakeScale(1.2, 1.2, 1.0)), // 11/30 + NSValue(caTransform3D: CATransform3DMakeScale(0.9, 0.9, 1.0)), // 14/30 + NSValue(caTransform3D: CATransform3DMakeScale(0.875, 0.875, 1.0)), // 15/30 + NSValue(caTransform3D: CATransform3DMakeScale(0.875, 0.875, 1.0)), // 16/30 + NSValue(caTransform3D: CATransform3DMakeScale(0.9, 0.9, 1.0)), // 17/30 + NSValue(caTransform3D: CATransform3DMakeScale(1.013, 1.013, 1.0)), // 20/30 + NSValue(caTransform3D: CATransform3DMakeScale(1.025, 1.025, 1.0)), // 21/30 + NSValue(caTransform3D: CATransform3DMakeScale(1.013, 1.013, 1.0)), // 22/30 + NSValue(caTransform3D: CATransform3DMakeScale(0.96, 0.96, 1.0)), // 25/30 + NSValue(caTransform3D: CATransform3DMakeScale(0.95, 0.95, 1.0)), // 26/30 + NSValue(caTransform3D: CATransform3DMakeScale(0.96, 0.96, 1.0)), // 27/30 + NSValue(caTransform3D: CATransform3DMakeScale(0.99, 0.99, 1.0)), // 29/30 + NSValue(caTransform3D: CATransform3DIdentity) // 30/30 ] imageTransform.keyTimes = [ 0.0, // 0/30 @@ -333,57 +335,57 @@ public class DOFavoriteButton: UIButton { 1.0 // 30/30 ] } - - private func addTargets() { + + fileprivate func addTargets() { //=============== // add target //=============== - self.addTarget(self, action: "touchDown:", forControlEvents: UIControlEvents.TouchDown) - self.addTarget(self, action: "touchUpInside:", forControlEvents: UIControlEvents.TouchUpInside) - self.addTarget(self, action: "touchDragExit:", forControlEvents: UIControlEvents.TouchDragExit) - self.addTarget(self, action: "touchDragEnter:", forControlEvents: UIControlEvents.TouchDragEnter) - self.addTarget(self, action: "touchCancel:", forControlEvents: UIControlEvents.TouchCancel) + self.addTarget(self, action: #selector(DOFavoriteButton.touchDown(_:)), for: UIControlEvents.touchDown) + self.addTarget(self, action: #selector(DOFavoriteButton.touchUpInside(_:)), for: UIControlEvents.touchUpInside) + self.addTarget(self, action: #selector(DOFavoriteButton.touchDragExit(_:)), for: UIControlEvents.touchDragExit) + self.addTarget(self, action: #selector(DOFavoriteButton.touchDragEnter(_:)), for: UIControlEvents.touchDragEnter) + self.addTarget(self, action: #selector(DOFavoriteButton.touchCancel(_:)), for: UIControlEvents.touchCancel) } - - func touchDown(sender: DOFavoriteButton) { + + func touchDown(_ sender: DOFavoriteButton) { self.layer.opacity = 0.4 } - func touchUpInside(sender: DOFavoriteButton) { + func touchUpInside(_ sender: DOFavoriteButton) { self.layer.opacity = 1.0 } - func touchDragExit(sender: DOFavoriteButton) { + func touchDragExit(_ sender: DOFavoriteButton) { self.layer.opacity = 1.0 } - func touchDragEnter(sender: DOFavoriteButton) { + func touchDragEnter(_ sender: DOFavoriteButton) { self.layer.opacity = 0.4 } - func touchCancel(sender: DOFavoriteButton) { + func touchCancel(_ sender: DOFavoriteButton) { self.layer.opacity = 1.0 } - - public func select() { - selected = true - imageShape.fillColor = imageColorOn.CGColor - + + open func select() { + isSelected = true + imageShape.fillColor = imageColorOn.cgColor + CATransaction.begin() - - circleShape.addAnimation(circleTransform, forKey: "transform") - circleMask.addAnimation(circleMaskTransform, forKey: "transform") - imageShape.addAnimation(imageTransform, forKey: "transform") - + + circleShape.add(circleTransform, forKey: "transform") + circleMask.add(circleMaskTransform, forKey: "transform") + imageShape.add(imageTransform, forKey: "transform") + for i in 0 ..< 5 { - lines[i].addAnimation(lineStrokeStart, forKey: "strokeStart") - lines[i].addAnimation(lineStrokeEnd, forKey: "strokeEnd") - lines[i].addAnimation(lineOpacity, forKey: "opacity") + lines[i].add(lineStrokeStart, forKey: "strokeStart") + lines[i].add(lineStrokeEnd, forKey: "strokeEnd") + lines[i].add(lineOpacity, forKey: "opacity") } - + CATransaction.commit() } - - public func deselect() { - selected = false - imageShape.fillColor = imageColorOff.CGColor - + + open func deselect() { + isSelected = false + imageShape.fillColor = imageColorOff.cgColor + // remove all animations circleShape.removeAllAnimations() circleMask.removeAllAnimations()