Skip to content

Commit

Permalink
fixed issue #4
Browse files Browse the repository at this point in the history
  • Loading branch information
okmr-d committed Jul 21, 2015
1 parent aa4dae4 commit 2f9bfe5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ class ViewController: UIViewController {
let y = self.view.frame.height / 2 - 22

// star button
let starButton = DOFavoriteButton(frame: CGRectMake(x, y, 44, 44))
let starButton = DOFavoriteButton(frame: CGRectMake(x, y, 44, 44), image: UIImage(named: "star", inBundle:NSBundle(forClass: DOFavoriteButton.self), compatibleWithTraitCollection: nil))
starButton.addTarget(self, action: Selector("tappedButton:"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(starButton)
x += width

// heart button
let heartButton = DOFavoriteButton(frame: CGRectMake(x, y, 44, 44), image: UIImage(named: "heart", inBundle:NSBundle(forClass: DOFavoriteButton.self), compatibleWithTraitCollection: nil), imageFrame: CGRectMake(12, 12, 20, 20))
let heartButton = DOFavoriteButton(frame: CGRectMake(x, y, 44, 44), image: UIImage(named: "heart", inBundle:NSBundle(forClass: DOFavoriteButton.self), compatibleWithTraitCollection: nil))
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)
Expand All @@ -36,7 +36,7 @@ class ViewController: UIViewController {
x += width

// like button
let likeButton = DOFavoriteButton(frame: CGRectMake(x, y, 44, 44), image: UIImage(named: "like", inBundle:NSBundle(forClass: DOFavoriteButton.self), compatibleWithTraitCollection: nil), imageFrame: CGRectMake(12, 12, 20, 20))
let likeButton = DOFavoriteButton(frame: CGRectMake(x, y, 44, 44), image: UIImage(named: "like", inBundle:NSBundle(forClass: DOFavoriteButton.self), compatibleWithTraitCollection: nil))
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)
Expand All @@ -45,7 +45,7 @@ class ViewController: UIViewController {
x += width

// smile button
let smileButton = DOFavoriteButton(frame: CGRectMake(x, y, 44, 44), image: UIImage(named: "smile", inBundle:NSBundle(forClass: DOFavoriteButton.self), compatibleWithTraitCollection: nil), imageFrame: CGRectMake(12, 12, 20, 20))
let smileButton = DOFavoriteButton(frame: CGRectMake(x, y, 44, 44), image: UIImage(named: "smile", inBundle:NSBundle(forClass: DOFavoriteButton.self), compatibleWithTraitCollection: nil))
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)
Expand Down
3 changes: 2 additions & 1 deletion DOFavoriteButton.podspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
Pod::Spec.new do |s|
s.name = "DOFavoriteButton"
s.version = "0.0.2"
s.version = "0.0.3"
s.summary = "Cute Animated Button written in Swift. It could be just right for favorite buttons!"
s.homepage = "https://github.com/okmr-d/DOFavoriteButton"
s.screenshots = "https://raw.githubusercontent.com/okmr-d/okmr-d.github.io/master/img/DOFavoriteButton/demo.gif"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Daiki Okumura" => "[email protected]" }
s.social_media_url = "http://twitter.com/okmr_d"
s.platform = :ios, "8.0"
s.source = { :git => "https://github.com/okmr-d/DOFavoriteButton.git", :tag => s.version.to_s }
s.source_files = "DOFavoriteButton/*.swift"
Expand Down
43 changes: 16 additions & 27 deletions DOFavoriteButton/DOFavoriteButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ import UIKit
@IBDesignable
public class DOFavoriteButton: UIButton {

private var imageShape: CAShapeLayer!
@IBInspectable public var image: UIImage! {
didSet {
let frame = self.frame
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)
createLayers(image: image, imageFrame: imageFrame)
imageShape.mask.contents = image.CGImage
}
}

private var imageShape: CAShapeLayer!
@IBInspectable public var imageColorOn: UIColor! = UIColor(red: 255/255, green: 172/255, blue: 51/255, alpha: 1.0) {
didSet {
if (selected) {
Expand All @@ -46,11 +43,11 @@ public class DOFavoriteButton: UIButton {
}
}

private var lines: [CAShapeLayer]! = []
private var lines: [CAShapeLayer]!
@IBInspectable public var lineColor: UIColor! = UIColor(red: 250/255, green: 120/255, blue: 68/255, alpha: 1.0) {
didSet {
for i in 0 ..< 5 {
lines[i].strokeColor = lineColor.CGColor
for line in lines {
line.strokeColor = lineColor.CGColor
}
}
}
Expand All @@ -73,7 +70,7 @@ public class DOFavoriteButton: UIButton {
}
}

@IBInspectable override public var selected : Bool {
override public var selected : Bool {
didSet {
if (selected != oldValue) {
if selected {
Expand All @@ -93,30 +90,22 @@ public class DOFavoriteButton: UIButton {
self.init(frame: frame, image: UIImage())
}

public convenience init(frame: CGRect, image: UIImage!) {
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)
self.init(frame: frame, image: image, imageFrame: imageFrame)
}

public init(frame: CGRect, image: UIImage!, imageFrame: CGRect) {
public init(frame: CGRect, image: UIImage!) {
super.init(frame: frame)
self.image = image
createLayers(image: image, imageFrame: imageFrame)
createLayers(image: image)
addTargets()
}

public required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
createLayers(image: UIImage())
addTargets()
}

public override func layoutSubviews() {
super.layoutSubviews()
//layoutLayers()
}

private func createLayers(#image: UIImage!, imageFrame: CGRect) {
private func createLayers(#image: UIImage!) {

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(imageFrame.origin.x + imageFrame.width / 2, imageFrame.origin.y + imageFrame.height / 2)
let lineFrame = CGRectMake(imageFrame.origin.x - imageFrame.width / 4, imageFrame.origin.y - imageFrame.height / 4 , imageFrame.width * 1.5, imageFrame.height * 1.5)

Expand Down Expand Up @@ -144,6 +133,7 @@ public class DOFavoriteButton: UIButton {
//===============
// line layer
//===============
lines = []
for i in 0 ..< 5 {
let line = CAShapeLayer()
line.bounds = lineFrame
Expand Down Expand Up @@ -180,11 +170,10 @@ public class DOFavoriteButton: UIButton {
imageShape.actions = ["fillColor": NSNull()]
self.layer.addSublayer(imageShape)

let imageMask = CALayer()
imageMask.contents = image.CGImage
imageMask.bounds = imageFrame
imageMask.position = imgCenterPoint
imageShape.mask = imageMask
imageShape.mask = CALayer()
imageShape.mask.contents = image.CGImage
imageShape.mask.bounds = imageFrame
imageShape.mask.position = imgCenterPoint

//==============================
// circle transform animation
Expand Down
22 changes: 10 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# DOFavoriteButton
[![Version](https://img.shields.io/cocoapods/v/DOFavoriteButton.svg?style=flat)](http://cocoapods.org/pods/DOFavoriteButton)
[![Platform](https://img.shields.io/cocoapods/p/DOFavoriteButton.svg?style=flat)](http://cocoapods.org/pods/DOFavoriteButton)
[![License](https://img.shields.io/cocoapods/l/DOFavoriteButton.svg?style=flat)](https://github.com/okmr-d/DOFavoriteButton/blob/master/LICENSE)

Cute Animated Button written in Swift.
It could be just right for favorite buttons!
Expand All @@ -23,24 +26,19 @@ Just drag DOFavoriteButton.swift to your project.
![Flat Icon Image](https://raw.githubusercontent.com/okmr-d/okmr-d.github.io/master/img/DOFavoriteButton/flatIconImage.png)

#### 2. Create a button
##### ・By using Storyboard or XIBs
##### ・By coding
```swift
let button = DOFavoriteButton(frame: CGRectMake(0, 0, 44, 44), image: UIImage(named: "star.png"))
self.view.addSubview(button)
```

##### ・By using Storyboard or XIB
1. Add Button object and set Custom Class `DOFavoriteButton`
![via Storyboard](https://raw.githubusercontent.com/okmr-d/okmr-d.github.io/master/img/DOFavoriteButton/storyboard.png)

2. Connect Outlet
![connect outlet](https://raw.githubusercontent.com/okmr-d/okmr-d.github.io/master/img/DOFavoriteButton/connect.png)

##### ・By coding
```swift
let button = DOFavoriteButton(
frame: CGRectMake(0, 0, 44, 44), // frame of button(tappable area) (blue frame in the picture below)
image: UIImage(named: "star.png"),
imageFrame: CGRectMake(12, 12, 20, 20) // frame of icon image (red frame in the picture below)
)
self.view.addSubview(button)
```
![Frames](https://raw.githubusercontent.com/okmr-d/okmr-d.github.io/master/img/DOFavoriteButton/frames.png)

#### 3. Add tapped function
```swift
button.addTarget(self, action: Selector("tapped:"), forControlEvents: .TouchUpInside)
Expand Down

0 comments on commit 2f9bfe5

Please sign in to comment.