Skip to content

Commit

Permalink
Add margin parameter
Browse files Browse the repository at this point in the history
Now you can set margin for spotlight
  • Loading branch information
aleksandrshoshiashvili committed Jun 14, 2017
1 parent 7db9d66 commit 379bf6f
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 22 deletions.
Empty file modified AwesomeSpotlightView/Classes/AwesomeSpotlight.swift
100644 → 100755
Empty file.
Empty file modified AwesomeSpotlightView/Classes/AwesomeSpotlightView.swift
100644 → 100755
Empty file.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0820"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "520617601E64AA6C008689A2"
BuildableName = "AwesomeSpotlightViewDemo.app"
BlueprintName = "AwesomeSpotlightViewDemo"
ReferencedContainer = "container:AwesomeSpotlightViewDemo.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "520617601E64AA6C008689A2"
BuildableName = "AwesomeSpotlightViewDemo.app"
BlueprintName = "AwesomeSpotlightViewDemo"
ReferencedContainer = "container:AwesomeSpotlightViewDemo.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "520617601E64AA6C008689A2"
BuildableName = "AwesomeSpotlightViewDemo.app"
BlueprintName = "AwesomeSpotlightViewDemo"
ReferencedContainer = "container:AwesomeSpotlightViewDemo.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "520617601E64AA6C008689A2"
BuildableName = "AwesomeSpotlightViewDemo.app"
BlueprintName = "AwesomeSpotlightViewDemo"
ReferencedContainer = "container:AwesomeSpotlightViewDemo.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>AwesomeSpotlightViewDemo.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>520617601E64AA6C008689A2</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

import UIKit



class AwesomeSpotlight: NSObject {

enum AwesomeSpotlightShape {
Expand All @@ -20,8 +18,10 @@ class AwesomeSpotlight: NSObject {

var rect = CGRect()
var shape : AwesomeSpotlightShape = .RoundRectangle
var margin = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
private var text : String = ""
private var attributedText : NSAttributedString? = nil
private let zeroMargin = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

var showedText: NSAttributedString {
if let attrText = attributedText {
Expand All @@ -35,22 +35,30 @@ class AwesomeSpotlight: NSObject {
return NSValue(cgRect: rect)
}

init(withRect rect: CGRect, shape: AwesomeSpotlightShape, text: String) {
init(withRect rect: CGRect,
shape: AwesomeSpotlightShape,
text: String,
margin: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)) {
super.init()
self.rect = rect
self.shape = shape
self.text = text
self.margin = margin
}

init(withRect rect: CGRect, shape: AwesomeSpotlightShape, attributedText: NSAttributedString) {
init(withRect rect: CGRect,
shape: AwesomeSpotlightShape,
attributedText: NSAttributedString,
margin: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)) {
super.init()
self.rect = rect
self.shape = shape
self.attributedText = attributedText
self.margin = margin
}

convenience override init() {
self.init(withRect: CGRect(), shape: .RoundRectangle, text: "test")
self.init(withRect: CGRect(), shape: .RoundRectangle, text: "test", margin: UIEdgeInsets())
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,21 @@ class AwesomeSpotlightView: UIView {
}

// MARK: - Touches

func userDidTap(_ recognizer: UITapGestureRecognizer) {
goToSpotlightAtIndex(index: currentIndex + 1)
}

// MARK: - Presenter

func start() {
alpha = 0
isHidden = false
textLabel.font = textLabelFont
UIView.animate(withDuration: animationDuration, animations: {
self.alpha = 1
}) { (finished) in
self.goToFirstSpotlight()
}) { (finished) in
self.goToFirstSpotlight()
}
}

Expand Down Expand Up @@ -231,7 +232,7 @@ class AwesomeSpotlightView: UIView {
setupContinueLabel()
UIView.animate(withDuration: animationDuration, delay: 0.35, options: .curveLinear, animations: {
self.continueLabel.alpha = 1
}, completion: nil)
}, completion: nil)
} else if index >= spotlightsArray.count - 1 && continueLabel.alpha != 0 {
continueLabel.alpha = 0
continueLabel.removeFromSuperview()
Expand All @@ -244,7 +245,7 @@ class AwesomeSpotlightView: UIView {
setupSkipSpotlightButton()
UIView.animate(withDuration: animationDuration, delay: 0.35, options: .curveLinear, animations: {
self.skipSpotlightButton.alpha = 1
}, completion: nil)
}, completion: nil)
}
}

Expand All @@ -258,6 +259,18 @@ class AwesomeSpotlightView: UIView {

// MARK: Helper

private func calculateRectWithMarginForSpotlight(_ spotlight: AwesomeSpotlight) -> CGRect {
var rect = spotlight.rect

rect.size.width += spotlight.margin.left + spotlight.margin.right
rect.size.height += spotlight.margin.bottom + spotlight.margin.top

rect.origin.x = rect.origin.x - (spotlight.margin.left + spotlight.margin.right) / 2.0
rect.origin.y = rect.origin.y - (spotlight.margin.top + spotlight.margin.bottom) / 2.0

return rect
}

private func calculateTextPositionAndSizeWithSpotlight(spotlight: AwesomeSpotlight) {
textLabel.frame = CGRect(x: 0, y: 0, width: maxLabelWidth, height: 0)
textLabel.attributedText = spotlight.showedText
Expand All @@ -268,7 +281,8 @@ class AwesomeSpotlightView: UIView {

textLabel.sizeToFit()

let rect = spotlight.rect
let rect = calculateRectWithMarginForSpotlight(spotlight)

var y = rect.origin.y + rect.size.height + labelSpacing
let bottomY = y + textLabel.frame.size.height + labelSpacing
if bottomY > bounds.size.height {
Expand All @@ -282,7 +296,7 @@ class AwesomeSpotlightView: UIView {
// MARK: - Cutout and Animate

private func cutoutToSpotlight(spotlight: AwesomeSpotlight, isFirst : Bool = false) -> UIBezierPath {
var rect = spotlight.rect
var rect = calculateRectWithMarginForSpotlight(spotlight)

if isFirst {
let x = floor(spotlight.rect.origin.x + (spotlight.rect.size.width / 2.0))
Expand Down Expand Up @@ -358,13 +372,13 @@ class AwesomeSpotlightView: UIView {
delegate?.spotlightViewWillCleanup?(spotlightView: self)
UIView.animate(withDuration: animationDuration, animations: {
self.alpha = 0
}) { (finished) in
self.removeFromSuperview()
self.currentIndex = 0
self.textLabel.alpha = 0
self.continueLabel.alpha = 0
self.skipSpotlightButton.alpha = 0
self.delegate?.spotlightViewDidCleanup?(spotlightView: self)
}) { (finished) in
self.removeFromSuperview()
self.currentIndex = 0
self.textLabel.alpha = 0
self.continueLabel.alpha = 0
self.skipSpotlightButton.alpha = 0
self.delegate?.spotlightViewDidCleanup?(spotlightView: self)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ class ViewController: UIViewController {
}

func setupSpotlight() {
let logoImageViewSpotlightRect = CGRect(x: logoImageView.frame.origin.x - 10, y: logoImageView.frame.origin.y - 10, width: logoImageView.frame.size.width + 18, height: logoImageView.frame.size.height + 18)
let logoImageViewSpotlight = AwesomeSpotlight(withRect: logoImageViewSpotlightRect, shape: .Circle, text: "logoImageViewSpotlight")
let logoImageViewSpotlightRect = CGRect(x: logoImageView.frame.origin.x, y: logoImageView.frame.origin.y, width: logoImageView.frame.size.width, height: logoImageView.frame.size.height)
let logoImageViewSpotlightMargin = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
let logoImageViewSpotlight = AwesomeSpotlight(withRect: logoImageViewSpotlightRect, shape: .Circle, text: "logoImageViewSpotlight", margin: logoImageViewSpotlightMargin)

let nameLabelSpotlight = AwesomeSpotlight(withRect: nameLabel.frame, shape: .Rectangle, text: "nameLabelSpotlight")

let showButtonSpotSpotlight = AwesomeSpotlight(withRect: showButton.frame, shape: .RoundRectangle, text: "showButtonSpotSpotlight")

let showWithContinueAndSkipButtonSpotlight = AwesomeSpotlight(withRect: showWithContinueAndSkipButton.frame, shape: .RoundRectangle, text: "showWithContinueAndSkipButtonSpotlight")

let showAllAtOnceButtonSpotlight = AwesomeSpotlight(withRect: showAllAtOnceButton.frame, shape: .RoundRectangle, text: "showAllAtOnceButtonSpotlight")

spotlightView = AwesomeSpotlightView(frame: view.frame, spotlight: [logoImageViewSpotlight, nameLabelSpotlight, showButtonSpotSpotlight, showWithContinueAndSkipButtonSpotlight, showAllAtOnceButtonSpotlight])
Expand Down

0 comments on commit 379bf6f

Please sign in to comment.