Skip to content
This repository has been archived by the owner on May 23, 2022. It is now read-only.

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
morizotter committed Jan 27, 2015
1 parent ecc083d commit 3814987
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 41 deletions.
73 changes: 32 additions & 41 deletions Pod/Classes/MZRPresentationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
import UIKit

public class MZRPresentationView: UIView {

// MARK: - Properties
private var enabled = false

private var image: UIImage?
private var color: UIColor?
private var touchViewSize = CGSizeMake(60.0, 60.0)
private var touchViews = [MZRTouchView]()

// MARK: - Life Cycle
Expand Down Expand Up @@ -63,26 +62,11 @@ public class MZRPresentationView: UIView {
public class func start() {
self.start(nil, image: nil)
}

public class func start(color: UIColor?, image: UIImage?) {
let instance = self.sharedInstance()
instance.enabled = true
instance.color = color

if (image != nil) {
instance.image = image
} else {
let color = instance.color ?? UIColor(red: 52/255.0, green: 152/255.0, blue: 219/255.0, alpha: 0.8)
let rect = CGRectMake(0, 0, instance.touchViewSize.width, instance.touchViewSize.height);
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
let contextRef = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(contextRef, color.CGColor)
CGContextFillEllipseInRect(contextRef, rect);
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
instance.image = image;
}
image?.imageWithRenderingMode(.AlwaysTemplate)

instance.image = image
if let window = UIApplication.sharedApplication().keyWindow {
for subview in window.subviews {
if (subview as? MZRTouchView != nil) {
Expand All @@ -94,32 +78,40 @@ public class MZRPresentationView: UIView {

public class func stop() {
let instance = self.sharedInstance()
instance.enabled = false
instance.removeFromSuperview()
}

public func handleEvnet(event: UIEvent) {

if event.type != UIEventType.Touches {
return
func dequeueTouchView() -> MZRTouchView {
var touchView: MZRTouchView?
for view in self.touchViews {
if view.superview == nil {
touchView = view
break
}
}

func createTouchView(touch: UITouch) -> UIImageView {
let view = MZRTouchView(frame: CGRectMake(0.0, 0.0, self.touchViewSize.width, self.touchViewSize.height))
view.touch = touch
view.image = self.image
view.tintColor = self.color
self.touchViews.append(view)
return view
if touchView == nil {
touchView = MZRTouchView(image: self.image, color: self.color)
self.touchViews.append(touchView!)
}

func findTouchView(touch: UITouch) -> MZRTouchView? {
for view in self.touchViews {
if view.touch == touch {
return view
}
touchView!.alpha = 1.0
return touchView!
}

func findTouchView(touch: UITouch) -> MZRTouchView? {
for view in self.touchViews {
if view.touch == touch {
return view
}
return nil
}
return nil
}

public func handleEvnet(event: UIEvent) {

if event.type != UIEventType.Touches {
return
}

let keyWindow = UIApplication.sharedApplication().keyWindow!
Expand All @@ -129,7 +121,8 @@ public class MZRPresentationView: UIView {
let phase = touch.phase
switch phase {
case .Began:
let view = createTouchView(touch)
let view = self.dequeueTouchView()
view.touch = touch
view.center = touch.locationInView(keyWindow)
keyWindow.addSubview(view)
case .Moved:
Expand All @@ -140,8 +133,6 @@ public class MZRPresentationView: UIView {
break
case .Ended, .Cancelled:
if let view = findTouchView(touch) {
let index = find(self.touchViews, view)
self.touchViews.removeAtIndex(index!)
UIView.animateWithDuration(0.2, delay: 0.0, options: .AllowUserInteraction, animations: { () -> Void in
view.alpha = 0.0
}, completion: { (finished) -> Void in
Expand Down
29 changes: 29 additions & 0 deletions Pod/Classes/MZRTouchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,33 @@ import UIKit

public class MZRTouchView: UIImageView {
weak var touch: UITouch?

let defaultTintColor = UIColor(red: 52/255.0, green: 152/255.0, blue: 219/255.0, alpha: 0.8)
let defaultImage: UIImage = {
let rect = CGRectMake(0, 0, 60.0, 60.0);
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
let contextRef = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(contextRef, UIColor.blackColor().CGColor)
CGContextFillEllipseInRect(contextRef, rect);
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
image.imageWithRenderingMode(.AlwaysTemplate)
return image
}()

convenience init(image: UIImage?, color: UIColor?) {
self.init(frame: CGRectMake(0.0, 0.0, 60.0, 60.0))

self.image = image ?? self.defaultImage
self.image = self.image?.imageWithRenderingMode(.AlwaysTemplate)
self.tintColor = color ?? self.defaultTintColor
}

override init(frame: CGRect) {
super.init(frame: frame)
}

required public init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

0 comments on commit 3814987

Please sign in to comment.