Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
110 changes: 50 additions & 60 deletions Classes/WobbleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,32 @@
import UIKit
import QuartzCore

public class WobbleView: UIView {
public class WobbleView: UIView, WobbleDelegate {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to use protocol implementation in extensions at EOF.

//on, off
public var on:Bool {
get {
return (layer as! WobbleLayer).on
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to don't use force casting

}
set(data) {
(layer as! WobbleLayer).on = data
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to don't use force casting

}
}

/*
The frequency of oscillation for the wobble behavior.
*/
The frequency of oscillation for the wobble behavior.
*/
@IBInspectable public var frequency: CGFloat = 3

/*
The amount of damping to apply to the wobble behavior.
*/
The amount of damping to apply to the wobble behavior.
*/
@IBInspectable public var damping: CGFloat = 0.3

/*
A bitmask value that identifies the edges that you want to wobble.
You can use this parameter to wobble only a subset of the edges of the rectangle.
*/
@IBInspectable public var edges: ViewEdge = .Right
A bitmask value that identifies the edges that you want to wobble.
You can use this parameter to wobble only a subset of the edges of the rectangle.
*/
@IBInspectable public var edges: ViewEdge = ViewEdge.All
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may short this to[...] = .All


// MARK: init
required public init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -53,35 +62,14 @@ public class WobbleView: UIView {
setUpDisplayLink()
}

public func reset() {

setUpMidpoints()
setUpCenters()
setUpBehaviours()

if vertexViews[0].layer.presentationLayer() != nil {

let bezierPath = UIBezierPath()
bezierPath.moveToPoint(vertexViews[0].layer.presentationLayer()!.frame.origin - layer.presentationLayer()!.frame.origin)
bezierPath.addLineToPoint(vertexViews[1].layer.presentationLayer()!.frame.origin - layer.presentationLayer()!.frame.origin)
bezierPath.addLineToPoint(vertexViews[2].layer.presentationLayer()!.frame.origin - layer.presentationLayer()!.frame.origin)
bezierPath.addLineToPoint(vertexViews[3].layer.presentationLayer()!.frame.origin - layer.presentationLayer()!.frame.origin)
bezierPath.closePath()

maskLayer.path = bezierPath.CGPath
(layer as! CAShapeLayer).path = bezierPath.CGPath
layer.mask = maskLayer
}
}

private func setUpVertices() {

vertexViews = []

let verticesOrigins = [CGPoint(x: frame.origin.x, y: frame.origin.y),
CGPoint(x: frame.origin.x + frame.width, y: frame.origin.y),
CGPoint(x: frame.origin.x + frame.width, y: frame.origin.y + frame.height),
CGPoint(x: frame.origin.x, y: frame.origin.y + frame.height)]
CGPoint(x: frame.origin.x + frame.width, y: frame.origin.y),
CGPoint(x: frame.origin.x + frame.width, y: frame.origin.y + frame.height),
CGPoint(x: frame.origin.x, y: frame.origin.y + frame.height)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-intent this 3 lines.


createAdditionalViews(&vertexViews, origins: verticesOrigins)
}
Expand All @@ -91,9 +79,9 @@ public class WobbleView: UIView {
midpointViews = []

let midpointsOrigins = [CGPoint(x: frame.origin.x + frame.width/2, y: frame.origin.y),
CGPoint(x: frame.origin.x + frame.width, y: frame.origin.y + frame.height/2),
CGPoint(x: frame.origin.x + frame.width/2, y: frame.origin.y + frame.height),
CGPoint(x: frame.origin.x, y: frame.origin.y + frame.height/2)]
CGPoint(x: frame.origin.x + frame.width, y: frame.origin.y + frame.height/2),
CGPoint(x: frame.origin.x + frame.width/2, y: frame.origin.y + frame.height),
CGPoint(x: frame.origin.x, y: frame.origin.y + frame.height/2)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-intent this 3 lines.


createAdditionalViews(&midpointViews, origins: midpointsOrigins)
}
Expand All @@ -102,12 +90,12 @@ public class WobbleView: UIView {

centerViews = []

let radius = min(frame.size.width/2, frame.size.height/2)
var radius = min(frame.size.width/2, frame.size.height/2)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we use var here?


let centersOrigins = [CGPoint(x: frame.origin.x + frame.width/2, y: frame.origin.y + radius),
CGPoint(x: frame.origin.x + frame.width - radius, y: frame.origin.y + frame.height/2),
CGPoint(x: frame.origin.x + frame.width/2, y: frame.origin.y + frame.height - radius),
CGPoint(x: frame.origin.x + radius, y: frame.origin.y + frame.height/2)]
CGPoint(x: frame.origin.x + frame.width - radius, y: frame.origin.y + frame.height/2),
CGPoint(x: frame.origin.x + frame.width/2, y: frame.origin.y + frame.height - radius),
CGPoint(x: frame.origin.x + radius, y: frame.origin.y + frame.height/2)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-intent this 3 lines.


createAdditionalViews(&centerViews, origins: centersOrigins)
}
Expand Down Expand Up @@ -149,10 +137,10 @@ public class WobbleView: UIView {

var bezierPath = UIBezierPath()
bezierPath.moveToPoint(vertexViews[0].layer.presentationLayer()!.frame.origin - layer.presentationLayer()!.frame.origin)
addEdge(&bezierPath, formerVertex: 0, latterVertex: 1, curved: edges.intersect(.Top))
addEdge(&bezierPath, formerVertex: 1, latterVertex: 2, curved: edges.intersect(.Right))
addEdge(&bezierPath, formerVertex: 2, latterVertex: 3, curved: edges.intersect(.Bottom))
addEdge(&bezierPath, formerVertex: 3, latterVertex: 0, curved: edges.intersect(.Left))
addEdge(&bezierPath, formerVertex: 0, latterVertex: 1, curved: ViewEdge(rawValue: edges.rawValue & ViewEdge.Top.rawValue))
addEdge(&bezierPath, formerVertex: 1, latterVertex: 2, curved: ViewEdge(rawValue: edges.rawValue & ViewEdge.Right.rawValue))
addEdge(&bezierPath, formerVertex: 2, latterVertex: 3, curved: ViewEdge(rawValue: edges.rawValue & ViewEdge.Bottom.rawValue))
addEdge(&bezierPath, formerVertex: 3, latterVertex: 0, curved: ViewEdge(rawValue: edges.rawValue & ViewEdge.Left.rawValue))
bezierPath.closePath()

maskLayer.path = bezierPath.CGPath
Expand All @@ -176,7 +164,7 @@ public class WobbleView: UIView {

for origin in origins {

let view = UIView(frame: CGRect(origin: origin, size: CGSize(width: 1, height: 1)))
var view = UIView(frame: CGRect(origin: origin, size: CGSize(width: 1, height: 1)))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we use var here?

view.backgroundColor = UIColor.clearColor()
addSubview(view)

Expand All @@ -186,7 +174,7 @@ public class WobbleView: UIView {

private func createAttachmentBehaviour(inout behaviours: [VertexAttachmentBehaviour], view: UIView, vertexIndex: Int) {

let attachmentBehaviour = VertexAttachmentBehaviour(item: view, attachedToAnchor: vertexViews[vertexIndex].frame.origin)
var attachmentBehaviour = VertexAttachmentBehaviour(item: view, attachedToAnchor: vertexViews[vertexIndex].frame.origin)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we use var here?

attachmentBehaviour.damping = damping
attachmentBehaviour.frequency = frequency
attachmentBehaviour.vertexIndex = vertexIndex
Expand All @@ -198,16 +186,15 @@ public class WobbleView: UIView {
private func addEdge(inout bezierPath: UIBezierPath, formerVertex: Int, latterVertex: Int, curved: ViewEdge) {

if (curved) {

let controlPoint = (vertexViews[formerVertex].layer.presentationLayer()!.frame.origin - (midpointViews[formerVertex].layer.presentationLayer()!.frame.origin - vertexViews[latterVertex].layer.presentationLayer()!.frame.origin)) - layer.presentationLayer()!.frame.origin
var controlPoint = ((vertexViews[formerVertex].layer.presentationLayer()?.frame.origin)! - ((midpointViews[formerVertex].layer.presentationLayer()?.frame.origin)! - (vertexViews[latterVertex].layer.presentationLayer()?.frame.origin)!)) - (layer.presentationLayer()?.frame.origin)!

bezierPath.addQuadCurveToPoint(vertexViews[latterVertex].layer.presentationLayer()!.frame.origin - layer.presentationLayer()!.frame.origin,
controlPoint: controlPoint)
controlPoint: controlPoint)

return;
}

bezierPath.addLineToPoint(vertexViews[latterVertex].layer.presentationLayer()!.frame.origin - layer.presentationLayer()!.frame.origin)
bezierPath.addLineToPoint((vertexViews[latterVertex].layer.presentationLayer()?.frame.origin)! - (layer.presentationLayer()?.frame.origin)!)
}

// MARK: private variables
Expand Down Expand Up @@ -241,27 +228,27 @@ extension WobbleView: UIDynamicAnimatorDelegate {
}

// MARK: WobbleDelegate
extension WobbleView: WobbleDelegate {
extension WobbleView {

func positionChanged() {

displayLink!.paused = false

let verticesOrigins = [CGPoint(x: frame.origin.x, y: frame.origin.y),
CGPoint(x: frame.origin.x + frame.width, y: frame.origin.y),
CGPoint(x: frame.origin.x + frame.width, y: frame.origin.y + frame.height),
CGPoint(x: frame.origin.x, y: frame.origin.y + frame.height)]
CGPoint(x: frame.origin.x + frame.width, y: frame.origin.y),
CGPoint(x: frame.origin.x + frame.width, y: frame.origin.y + frame.height),
CGPoint(x: frame.origin.x, y: frame.origin.y + frame.height)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-intent this 3 lines.


for (i, vertexView) in vertexViews.enumerate() {
for (i, vertexView) in vertexViews.enumerate() {
vertexView.frame.origin = verticesOrigins[i]
}

let radius = min(frame.size.width/2, frame.size.height/2)
var radius = min(frame.size.width/2, frame.size.height/2)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we use var here?


let centersOrigins = [CGPoint(x: frame.origin.x + frame.width/2, y: frame.origin.y + radius),
CGPoint(x: frame.origin.x + frame.width - radius, y: frame.origin.y + frame.height/2),
CGPoint(x: frame.origin.x + frame.width/2, y: frame.origin.y + frame.height - radius),
CGPoint(x: frame.origin.x + radius, y: frame.origin.y + frame.height/2)]
CGPoint(x: frame.origin.x + frame.width - radius, y: frame.origin.y + frame.height/2),
CGPoint(x: frame.origin.x + frame.width/2, y: frame.origin.y + frame.height - radius),
CGPoint(x: frame.origin.x + radius, y: frame.origin.y + frame.height/2)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-intent this 3 lines.


for (i, centerView) in centerViews.enumerate() {
centerView.frame.origin = centersOrigins[i]
Expand All @@ -286,12 +273,15 @@ private protocol WobbleDelegate {
}

private class WobbleLayer: CAShapeLayer {
var on:Bool = false

var wobbleDelegate: WobbleDelegate?

@objc override var position: CGPoint {
didSet {
wobbleDelegate?.positionChanged()
if on {
wobbleDelegate?.positionChanged()
}
}
}
}
Expand Down
Binary file added Example/.DS_Store
Binary file not shown.
6 changes: 4 additions & 2 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,9 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
EMBEDDED_CONTENT_CONTAINS_SWIFT = NO;
INFOPLIST_FILE = Example/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 9.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.infullmobile.ui.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -285,8 +286,9 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
EMBEDDED_CONTENT_CONTAINS_SWIFT = NO;
INFOPLIST_FILE = Example/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 9.2;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.infullmobile.ui.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
6 changes: 3 additions & 3 deletions Example/Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8191" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="6Ut-IV-X96">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15C50" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="6Ut-IV-X96">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -27,7 +27,7 @@
<rect key="frame" x="0.0" y="22" width="320" height="90"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="M6W-gc-8mr" id="doq-8d-zLG">
<rect key="frame" x="0.0" y="0.0" width="320" height="89.5"/>
<rect key="frame" x="0.0" y="0.0" width="320" height="89"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="UGt-C6-nsL" customClass="WobbleView" customModule="Example" customModuleProvider="target">
Expand Down