diff --git a/DOFavoriteButton-DEMO/.DS_Store b/DOFavoriteButton-DEMO/.DS_Store new file mode 100644 index 0000000..e7b6c7c Binary files /dev/null and b/DOFavoriteButton-DEMO/.DS_Store differ diff --git a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO.xcodeproj/project.pbxproj b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO.xcodeproj/project.pbxproj index 870b57b..3db2618 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 */; }; + 8465A9631D9C60B500B1D8C4 /* DOFavoriteButton.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4F60711B1B6366ED0058F570 /* DOFavoriteButton.framework */; }; + 8465A9641D9C60B500B1D8C4 /* 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 */ + 8465A9651D9C60B500B1D8C4 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 4FBAA6F51B4D807F00C8657F /* Project object */; + proxyType = 1; + remoteGlobalIDString = 4F60711A1B6366ED0058F570; + remoteInfo = DOFavoriteButton; + }; +/* End PBXContainerItemProxy section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 8465A9671D9C60B500B1D8C4 /* Embed Frameworks */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = ""; + dstSubfolderSpec = 10; + files = ( + 8465A9641D9C60B500B1D8C4 /* 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 = ( + 8465A9631D9C60B500B1D8C4 /* DOFavoriteButton.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -157,10 +184,12 @@ 4FBAA6F91B4D807F00C8657F /* Sources */, 4FBAA6FA1B4D807F00C8657F /* Frameworks */, 4FBAA6FB1B4D807F00C8657F /* Resources */, + 8465A9671D9C60B500B1D8C4 /* Embed Frameworks */, ); buildRules = ( ); dependencies = ( + 8465A9661D9C60B500B1D8C4 /* PBXTargetDependency */, ); name = "DOFavoriteButton-DEMO"; productName = "DOFavoriteButton-DEMO"; @@ -175,14 +204,16 @@ attributes = { LastSwiftMigration = 0700; LastSwiftUpdateCheck = 0700; - LastUpgradeCheck = 0700; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = "Daiki Okumura"; TargetAttributes = { 4F60711A1B6366ED0058F570 = { CreatedOnToolsVersion = 6.4; + LastSwiftMigration = 0800; }; 4FBAA6FC1B4D807F00C8657F = { CreatedOnToolsVersion = 6.4; + LastSwiftMigration = 0800; }; }; }; @@ -245,6 +276,14 @@ }; /* End PBXSourcesBuildPhase section */ +/* Begin PBXTargetDependency section */ + 8465A9661D9C60B500B1D8C4 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 4F60711A1B6366ED0058F570 /* DOFavoriteButton */; + targetProxy = 8465A9651D9C60B500B1D8C4 /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + /* Begin PBXVariantGroup section */ 4FBAA7061B4D807F00C8657F /* Main.storyboard */ = { isa = PBXVariantGroup; @@ -269,6 +308,7 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ENABLE_MODULES = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; CURRENT_PROJECT_VERSION = 1; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; @@ -286,6 +326,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -295,6 +336,7 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ENABLE_MODULES = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; CURRENT_PROJECT_VERSION = 1; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; @@ -307,6 +349,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "co.devlog.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; + SWIFT_VERSION = 3.0; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -325,8 +368,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -371,8 +416,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -391,6 +438,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; }; @@ -399,24 +447,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; }; 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; }; name = Release; }; diff --git a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO.xcodeproj/xcshareddata/xcschemes/DOFavoriteButton.xcscheme b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO.xcodeproj/xcshareddata/xcschemes/DOFavoriteButton.xcscheme index ab1a5ab..f5b5fb5 100644 --- a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO.xcodeproj/xcshareddata/xcschemes/DOFavoriteButton.xcscheme +++ b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO.xcodeproj/xcshareddata/xcschemes/DOFavoriteButton.xcscheme @@ -1,6 +1,6 @@ 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/Base.lproj/LaunchScreen.xib b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/Base.lproj/LaunchScreen.xib index cbcb182..bb58b81 100644 --- a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/Base.lproj/LaunchScreen.xib +++ b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/Base.lproj/LaunchScreen.xib @@ -1,9 +1,10 @@ - + - + + @@ -13,22 +14,17 @@ - - + diff --git a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/Base.lproj/Main.storyboard b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/Base.lproj/Main.storyboard index 280b163..a5f05b1 100644 --- a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/Base.lproj/Main.storyboard +++ b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/Base.lproj/Main.storyboard @@ -1,8 +1,9 @@ - + - + + @@ -14,42 +15,37 @@ - + - - + diff --git a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/Images.xcassets/AppIcon.appiconset/Contents.json b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/Images.xcassets/AppIcon.appiconset/Contents.json index 36d2c80..1d060ed 100644 --- a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/Images.xcassets/AppIcon.appiconset/Contents.json +++ b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/Images.xcassets/AppIcon.appiconset/Contents.json @@ -1,5 +1,15 @@ { "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, { "idiom" : "iphone", "size" : "29x29", @@ -30,6 +40,16 @@ "size" : "60x60", "scale" : "3x" }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "1x" + }, + { + "idiom" : "ipad", + "size" : "20x20", + "scale" : "2x" + }, { "idiom" : "ipad", "size" : "29x29", @@ -59,6 +79,11 @@ "idiom" : "ipad", "size" : "76x76", "scale" : "2x" + }, + { + "idiom" : "ipad", + "size" : "83.5x83.5", + "scale" : "2x" } ], "info" : { diff --git a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/ViewController.swift b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/ViewController.swift index 99be5ed..910f2ee 100644 --- a/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/ViewController.swift +++ b/DOFavoriteButton-DEMO/DOFavoriteButton-DEMO/ViewController.swift @@ -21,38 +21,38 @@ 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() { @@ -60,8 +60,8 @@ class ViewController: UIViewController { // Dispose of any resources that can be recreated. } - func tappedButton(sender: DOFavoriteButton) { - if sender.selected { + func tappedButton(_ sender: DOFavoriteButton) { + if sender.isSelected { sender.deselect() } else { sender.select() diff --git a/DOFavoriteButton/DOFavoriteButton.swift b/DOFavoriteButton/DOFavoriteButton.swift index 4b7e9b8..3eabea5 100644 --- a/DOFavoriteButton/DOFavoriteButton.swift +++ b/DOFavoriteButton/DOFavoriteButton.swift @@ -12,54 +12,54 @@ import UIKit @IBDesignable -public class DOFavoriteButton: UIButton { +open class DOFavoriteButton: UIButton { - private var imageShape: CAShapeLayer! - @IBInspectable public var image: UIImage! { + 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") + 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 public var duration: Double = 1.0 { + @IBInspectable open var duration: Double = 1.0 { didSet { circleTransform.duration = 0.333 * duration // 0.0333 * 10 circleMaskTransform.duration = 0.333 * duration // 0.0333 * 10 @@ -70,11 +70,11 @@ public class DOFavoriteButton: UIButton { } } - 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() } @@ -83,7 +83,7 @@ public class DOFavoriteButton: UIButton { } public convenience init() { - self.init(frame: CGRectZero) + self.init(frame: CGRect.zero) } public override convenience init(frame: CGRect) { @@ -103,12 +103,12 @@ public class DOFavoriteButton: UIButton { 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 @@ -116,8 +116,8 @@ public class DOFavoriteButton: UIButton { 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) @@ -128,8 +128,8 @@ public class DOFavoriteButton: UIButton { 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,13 +141,15 @@ 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)) + //CGPathMoveToPoint(path, nil, lineFrame.midX, lineFrame.midY) + path.addLine(to:CGPoint(x: lineFrame.origin.x + lineFrame.width / 2, y: lineFrame.origin.y)) + //CGPathAddLineToPoint(path, nil, lineFrame.origin.x + lineFrame.width / 2, lineFrame.origin.y) return path }() line.lineCap = kCALineCapRound @@ -166,13 +168,13 @@ public class DOFavoriteButton: UIButton { 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 @@ -181,14 +183,14 @@ public class DOFavoriteButton: UIButton { //============================== 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 @@ -203,15 +205,15 @@ public class DOFavoriteButton: UIButton { 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 @@ -295,23 +297,23 @@ public class DOFavoriteButton: UIButton { //============================== 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 @@ -334,55 +336,55 @@ public class DOFavoriteButton: UIButton { ] } - 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()