Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions TTFortuneWheel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@
CODE_SIGN_IDENTITY = "";
CODE_SIGN_STYLE = Automatic;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = G7T599BH7B;
DEVELOPMENT_TEAM = 25V5D4DNE7;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand All @@ -491,7 +491,7 @@
CODE_SIGN_IDENTITY = "";
CODE_SIGN_STYLE = Automatic;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = G7T599BH7B;
DEVELOPMENT_TEAM = 25V5D4DNE7;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand All @@ -513,7 +513,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = G7T599BH7B;
DEVELOPMENT_TEAM = 25V5D4DNE7;
INFOPLIST_FILE = TTFortuneWheelSample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -530,7 +530,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = G7T599BH7B;
DEVELOPMENT_TEAM = 25V5D4DNE7;
INFOPLIST_FILE = TTFortuneWheelSample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down
65 changes: 45 additions & 20 deletions TTFortuneWheel/FortuneWheelLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ open class FortuneWheelLayer: CALayer {
var mainFrame:CGRect!
weak var parent:TTFortuneWheel!
private var initialOffset:CGFloat!

public init(frame:CGRect, parent:TTFortuneWheel,initialOffset:CGFloat = 0.0) {

public init(frame:CGRect,
parent:TTFortuneWheel,
initialOffset:CGFloat = 0.0) {

super.init()
mainFrame = CGRect(origin: CGPoint(x: abs(layerInsets.left), y: abs(layerInsets.top)), size: frame.size)
mainFrame = CGRect(origin: CGPoint(x: abs(layerInsets.left),
y: abs(layerInsets.top)),
size: frame.size)
self.frame = frame.inset(by: layerInsets)
self.parent = parent
self.initialOffset = initialOffset
Expand All @@ -50,11 +55,13 @@ open class FortuneWheelLayer: CALayer {
open func drawCanvas(mainFrame: CGRect) {
//// General Declarations
let context = UIGraphicsGetCurrentContext()!

//// Main Group
context.saveGState()
if let shadow = parent.shadow {
context.setShadow(offset: shadow.shadowOffset, blur: shadow.shadowBlurRadius, color: (shadow.shadowColor as! UIColor).cgColor)
context.setShadow(offset: shadow.shadowOffset,
blur: shadow.shadowBlurRadius,
color: (shadow.shadowColor as! UIColor).cgColor)
}
context.beginTransparencyLayer(auxiliaryInfo: nil)

Expand All @@ -64,13 +71,17 @@ open class FortuneWheelLayer: CALayer {
if let previousSlice = parent.slices[safe:(index - 1)] {
rotation += (degree(of:previousSlice) + degree(of:element)) / 2
}
self.drawSlice(withIndex: index, in: context, forSlice: element,rotation:rotation)

self.drawSlice(withIndex: index,
in: context,
forSlice: element,
rotation:rotation)
}

//// Aditional graphics drwings
parent.slices.enumerated().forEach { (index,element) in
self.drawAdditionalGraphics(in: context, rotation: rotation, for: element)
self.drawAdditionalGraphics(in: context,
rotation: rotation,
for: element)
let previousSlice:FortuneWheelSliceProtocol = parent.slices[safe:(index - 1)] ?? element
rotation += degree(of:previousSlice)
}
Expand All @@ -95,7 +106,7 @@ open class FortuneWheelLayer: CALayer {
}

//MARK:- Graphics drawings

open func drawSlice(withIndex index:Int, in context:CGContext, forSlice slice:FortuneWheelSliceProtocol, rotation:CGFloat) {

///// Constats declarations
Expand All @@ -117,9 +128,17 @@ open class FortuneWheelLayer: CALayer {
context.rotate(by: rotation * CGFloat.pi/180)

//// Slice drawing
let sliceRect = CGRect(x: (mainFrame.minX - rotationOffset), y: (mainFrame.minY - rotationOffset), width: mainFrame.width, height: mainFrame.height)
let sliceRect = CGRect(x: (mainFrame.minX - rotationOffset),
y: (mainFrame.minY - rotationOffset),
width: mainFrame.width,
height: mainFrame.height)
let slicePath = UIBezierPath()
slicePath.addArc(withCenter: CGPoint(x: sliceRect.midX, y: sliceRect.midY), radius: sliceRect.width / 2, startAngle: -startAngle * CGFloat.pi/180, endAngle: -endAngle * CGFloat.pi/180, clockwise: true)
slicePath.addArc(withCenter: CGPoint(x: sliceRect.midX, y: sliceRect.midY),
radius: sliceRect.width / 2,
startAngle: -startAngle * CGFloat.pi/180,
endAngle: -endAngle * CGFloat.pi/180,
clockwise: true)

slicePath.addLine(to: CGPoint(x: sliceRect.midX, y: sliceRect.midY))
slicePath.close()
slice.backgroundColor?.setFill()
Expand All @@ -133,7 +152,10 @@ open class FortuneWheelLayer: CALayer {
}

//// Title Drawing
let textRect = CGRect(x: (titleXValue - rotationOffset), y: (titleYPosition - rotationOffset), width: titleWidthValue, height: titleHeightValue)
let textRect = CGRect(x: (titleXValue - rotationOffset),
y: (titleYPosition - rotationOffset),
width: titleWidthValue,
height: titleHeightValue)
let textTextContent = slice.title

//// Set title attributes
Expand All @@ -145,9 +167,12 @@ open class FortuneWheelLayer: CALayer {
let textTextHeight: CGFloat = textTextContent.boundingRect(with: CGSize(width: textRect.width, height: CGFloat.infinity), options: .usesLineFragmentOrigin, attributes: textFontAttributes, context: nil).height
context.saveGState()
context.clip(to: textRect)
textTextContent.draw(in: CGRect(x: textRect.minX, y: textRect.minY + (textRect.height - textTextHeight) / 2, width: textRect.width, height: textTextHeight), withAttributes: textFontAttributes)
context.restoreGState()

textTextContent.draw(in: CGRect(x: textRect.minX,
y: textRect.minY + (textRect.height - textTextHeight) / 2,
width: textRect.width,
height: textTextHeight),
withAttributes: textFontAttributes)

context.restoreGState()
}

Expand All @@ -158,11 +183,11 @@ open class FortuneWheelLayer: CALayer {
context.saveGState()
context.translateBy(x: rotationOffset, y: rotationOffset)
context.rotate(by: rotation * CGFloat.pi/180)
slice.drawAdditionalGraphics(in: context,circularSegmentHeight:circularSegmentHeight, radius: radius,sliceDegree:sectionWidthDegrees)
context.restoreGState()
slice.drawAdditionalGraphics(in: context,
circularSegmentHeight:circularSegmentHeight,
radius: radius,
sliceDegree:sectionWidthDegrees)

context.restoreGState()
}



}
37 changes: 28 additions & 9 deletions TTFortuneWheel/SpinningWheelAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class SpinningWheelAnimator : NSObject, CAAnimationDelegate {

}

func addRotationAnimation(completionBlock: ((_ finished: Bool) -> Void)? = nil, rotationOffset:CGFloat = 0.0){
func addRotationAnimation(completionBlock: ((_ finished: Bool) -> Void)? = nil,
rotationOffset:CGFloat = 0.0){
if completionBlock != nil{
let completionAnim = CABasicAnimation(keyPath:"completionAnim")
completionAnim.duration = rotationTime
Expand All @@ -69,18 +70,25 @@ class SpinningWheelAnimator : NSObject, CAAnimationDelegate {
starTransformAnim.values = [0, rotation * CGFloat.pi/180]
starTransformAnim.keyTimes = [0, 1]
starTransformAnim.duration = 5
starTransformAnim.timingFunction = CAMediaTimingFunction(controlPoints: 0.0256, 0.874, 0.675, 1)
starTransformAnim.timingFunction = CAMediaTimingFunction(controlPoints: 0.0256,
0.874,
0.675,
1)

let starRotationAnim : CAAnimationGroup = TTUtils.group(animations: [starTransformAnim], fillMode:fillMode)
animationObject.layerToAnimate.add(starRotationAnim, forKey:"starRotationAnim")
let starRotationAnim: CAAnimationGroup = TTUtils.group(animations: [starTransformAnim],
fillMode:fillMode)
animationObject
.layerToAnimate
.add(starRotationAnim, forKey:"starRotationAnim")
}

//MARK: - Animation Cleanup

func animationDidStop(_ anim: CAAnimation, finished flag: Bool){
if let completionBlock = completionBlocks[anim]{
completionBlocks.removeValue(forKey: anim)
if (flag && updateLayerValueForCompletedAnimation) || anim.value(forKey: "needEndAnim") as! Bool{
if (flag && updateLayerValueForCompletedAnimation) ||
anim.value(forKey: "needEndAnim") as! Bool{
updateLayerValues(forAnimationId: anim.value(forKey: "animId") as! String)
removeAnimations(forAnimationId: anim.value(forKey: "animId") as! String)
}
Expand All @@ -90,22 +98,33 @@ class SpinningWheelAnimator : NSObject, CAAnimationDelegate {

func updateLayerValues(forAnimationId identifier: String){
if identifier == "rotation"{
TTUtils.updateValueFromPresentationLayer(forAnimation: animationObject.layerToAnimate.animation(forKey: "starRotationAnim"), theLayer:animationObject.layerToAnimate)
TTUtils
.updateValueFromPresentationLayer(forAnimation:
animationObject
.layerToAnimate
.animation(forKey: "starRotationAnim"),
theLayer:animationObject.layerToAnimate)
}
}

func removeAnimations(forAnimationId identifier: String){
if identifier == "rotation"{
animationObject.layerToAnimate.removeAnimation(forKey: "starRotationAnim")
animationObject
.layerToAnimate
.removeAnimation(forKey: "starRotationAnim")
}
}

func removeIndefiniteAnimation() {
animationObject.layerToAnimate.removeAnimation(forKey: "starRotationIndefiniteAnim")
animationObject
.layerToAnimate
.removeAnimation(forKey: "starRotationIndefiniteAnim")
}

func removeAllAnimations(){
animationObject.layerToAnimate.removeAllAnimations()
animationObject
.layerToAnimate
.removeAllAnimations()
}

}
8 changes: 6 additions & 2 deletions TTFortuneWheel/SpinningWheelSlice .swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,15 @@ public protocol FortuneWheelSliceProtocol {

extension FortuneWheelSliceProtocol {

public func drawAdditionalGraphics(in context:CGContext, circularSegmentHeight:CGFloat,radius:CGFloat,sliceDegree:CGFloat) { }
public func drawAdditionalGraphics(in context:CGContext,
circularSegmentHeight:CGFloat,
radius:CGFloat,
sliceDegree:CGFloat) { }

public var fontSize:CGFloat { return 18.0 }
public var fontColor:UIColor { return UIColor.black }
public var font:UIFont { return UIFont.systemFont(ofSize: fontSize, weight: .regular) }
public var font:UIFont { return UIFont.systemFont(ofSize: fontSize,
weight: .regular) }

public var textAttributes:[NSAttributedString.Key:Any] {
let textStyle = NSMutableParagraphStyle()
Expand Down
27 changes: 21 additions & 6 deletions TTFortuneWheel/SpinningWheelSliceImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public class FortuneWheelSlice: FortuneWheelSliceProtocol {
}

public var font: UIFont {
return UIFont.systemFont(ofSize: fontSize, weight: .bold)
return UIFont.systemFont(ofSize: fontSize,
weight: .bold)
}

public var style:Style = .dark
Expand All @@ -44,20 +45,34 @@ public class FortuneWheelSlice: FortuneWheelSliceProtocol {
self.title = title
}

public convenience init(title:String, degree:CGFloat) {
public convenience init(title:String,
degree:CGFloat) {
self.init(title:title)
self.degree = degree
}

public func drawAdditionalGraphics(in context:CGContext, circularSegmentHeight:CGFloat,radius:CGFloat,sliceDegree:CGFloat) {
let image = UIImage(named: "niddleImage", in: Bundle.sw_frameworkBundle(), compatibleWith: nil)!
public func drawAdditionalGraphics(in context:CGContext,
circularSegmentHeight:CGFloat,
radius:CGFloat,
sliceDegree:CGFloat) {
let image = UIImage(named: "niddleImage",
in: Bundle.sw_frameworkBundle(),
compatibleWith: nil)!
let centerOffset = CGPoint(x: -64, y: 17)
let additionalGraphicRect = CGRect(x: centerOffset.x, y: centerOffset.y, width: 12, height: 12)
let additionalGraphicRect = CGRect(x: centerOffset.x,
y: centerOffset.y,
width: 12,
height: 12)
let additionalGraphicPath = UIBezierPath(rect: additionalGraphicRect)
context.saveGState()
additionalGraphicPath.addClip()
context.scaleBy(x: 1, y: -1)
context.draw(image.cgImage!, in: CGRect(x: additionalGraphicRect.minX, y: -additionalGraphicRect.minY, width: image.size.width, height: image.size.height), byTiling: true)
context.draw(image.cgImage!,
in: CGRect(x: additionalGraphicRect.minX,
y: -additionalGraphicRect.minY,
width: image.size.width,
height: image.size.height),
byTiling: true)
context.restoreGState()
}
}
11 changes: 9 additions & 2 deletions TTFortuneWheel/TTFortuneWheel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public class TTFortuneWheel: UIControl, CAAnimationDelegate, SpinningAnimatorPro
}

private func addWheelLayer() {
wheelLayer = FortuneWheelLayer(frame:self.bounds,parent:self,initialOffset:initialDrawingOffset)
wheelLayer = FortuneWheelLayer(frame:self.bounds,
parent:self,
initialOffset:initialDrawingOffset)
self.layer.addSublayer(wheelLayer)
wheelLayer.setNeedsDisplay()
}
Expand Down Expand Up @@ -110,4 +112,9 @@ public class TTFortuneWheel: UIControl, CAAnimationDelegate, SpinningAnimatorPro
}
}


extension TTFortuneWheel {
open func reload(slices: [FortuneWheelSliceProtocol]) {
self.slices = slices
draw(self.bounds)
}
}
Loading