Skip to content

Commit

Permalink
Finish to implements the custom image mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
safety97 committed Aug 7, 2018
1 parent 1af000d commit ce8a3e9
Show file tree
Hide file tree
Showing 43 changed files with 217 additions and 61 deletions.
61 changes: 39 additions & 22 deletions Example/StarsKit/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions Example/StarsKit/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ViewController: UIViewController {

@IBOutlet weak var ibSessionintervalChecking: UISwitch!


override func viewDidLoad() {
super.viewDidLoad()

Expand All @@ -71,6 +72,16 @@ class ViewController: UIViewController {
// rect: CGRect(x: 0, y: 0, width: 50, height: 50))
let bgImage = UIColor.ex.fromHexa("#42aaf4")?.ex.toImage()
StarsKit.shared.graphicContext.backgroundHeaderTitleImage = bgImage

if let image1 = UIImage(named: "ic_notation_1",in: Bundle(for: StarsKit.self),
compatibleWith: nil), let image2 = UIImage(named: "ic_notation_2",in: Bundle(for: StarsKit.self),
compatibleWith: nil), let image3 = UIImage(named: "ic_notation_3",in: Bundle(for: StarsKit.self),
compatibleWith: nil), let image4 = UIImage(named: "ic_notation_4",in: Bundle(for: StarsKit.self),
compatibleWith: nil), let image5 = UIImage(named: "ic_notation_5",in: Bundle(for: StarsKit.self),
compatibleWith: nil) {
StarsKit.shared.graphicContext.customImages = [image1, image2, image3, image4, image5]
}

StarsKit.shared.graphicContext.actionButtonBackgroundColor = UIColor.ex.fromHexa("#007BFF")!
StarsKit.shared.delegate = self
StarsKit.shared.uiDelegate = self
Expand Down Expand Up @@ -107,6 +118,10 @@ class ViewController: UIViewController {

}

@IBAction func didSwitchCustomImageMode(_ sender: UISwitch) {
StarsKit.shared.customImageMode = sender.isOn
}

@IBAction func didSwitchNativeRating(_ sender: UISwitch) {
StarsKit.shared.priorityUseNativeRate = sender.isOn
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_notation_1.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_notation_2.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_notation_3.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_notation_4.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_notation_5.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions StarsKit/Classes/Logic/StarsKitGraphicContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ public class StarsKitGraphicContext {
in: Bundle(for: StarsKit.self),
compatibleWith: nil)

public lazy var customImages: [UIImage]? = [UIImage(named: "shape_selected",
in: Bundle(for: StarsKit.self),
compatibleWith: nil)]
public var customImages: [UIImage]?

public lazy var preferredStatusBarStyle = UIStatusBarStyle.lightContent
public lazy var stepTransitionOptions: UIViewAnimationOptions = .transitionCrossDissolve

Expand Down
32 changes: 22 additions & 10 deletions StarsKit/Classes/UI/SmileyRateView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,53 @@

import UIKit


@available(iOS 9.0, *)
class SmileyRateView: UIView {

@IBOutlet var ibStackView: UIStackView!

var images: [UIImage]?
var rating: Double = 0
private let nibName = "SmileyRateView"
private var view: UIView!

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

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
xibSetUp()
}

func xibSetUp() {
view = loadViewFromNib()
view.frame = self.bounds
view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
addSubview(view)
}

private func commonInit() {
Bundle.main.loadNibNamed("SmileyRateView", owner: self, options: nil)
func loadViewFromNib() -> UIView? {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
let view: UIView? = nib.instantiate(withOwner: self, options: nil)[0] as? UIView ?? nil
return view
}

/// Closure will be called when the user lifts finger from the cosmos view. The touch rating argument is passed to the closure.
open var didFinishTouching: ((Double)->())?
open var didFinishTouching: ((Double) -> Void)?

func computeView () {

ibStackView.spacing = 0
self.ibStackView.spacing = 10
var index = 1
images?.forEach { (image: UIImage) in
let button = UIButton(type: .custom)
button.setImage(image, for: .normal)
button.alpha = 1
button.tag = index
button.imageView?.contentMode = .scaleAspectFit
index += 1
button.addTarget(self, action: #selector(SmileyRateView.selectRate(_:)), for: UIControlEvents.touchUpInside)
self.ibStackView.addArrangedSubview(button)
Expand All @@ -48,7 +61,6 @@ class SmileyRateView: UIView {
}

@objc private func selectRate(_ sender: UIButton) {

if !sender.isSelected {
self.ibStackView.arrangedSubviews.forEach { button in
if let button = button as? UIButton, button != sender {
Expand All @@ -60,10 +72,10 @@ class SmileyRateView: UIView {
}
}
self.animateButton(button: sender)
didFinishTouching?(Double(sender.tag))
self.rating = Double(sender.tag)
self.didFinishTouching?(Double(sender.tag))
}
sender.isSelected = true

}

private func animateButton(button: UIButton) {
Expand Down
15 changes: 7 additions & 8 deletions StarsKit/Classes/UI/SmileyRateView.xib
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,29 @@
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="SmileyRateView" customModuleProvider="target">
<connections>
<outlet property="ibStackView" destination="BQP-at-d9u" id="vzB-j1-h8q"/>
<outlet property="ibStackView" destination="BQP-at-d9u" id="GIV-Uw-Hpp"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB">
<rect key="frame" x="0.0" y="0.0" width="408" height="155"/>
<rect key="frame" x="0.0" y="0.0" width="408" height="136"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" distribution="fillEqually" alignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="BQP-at-d9u">
<rect key="frame" x="0.0" y="52.5" width="408" height="50"/>
<constraints>
<constraint firstAttribute="height" constant="50" id="ea1-Cs-uJH"/>
</constraints>
<stackView opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="1000" verticalHuggingPriority="1000" horizontalCompressionResistancePriority="1000" verticalCompressionResistancePriority="1000" distribution="fillEqually" alignment="center" spacing="40" translatesAutoresizingMaskIntoConstraints="NO" id="BQP-at-d9u">
<rect key="frame" x="0.0" y="0.0" width="408" height="136"/>
</stackView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="vUN-kp-3ea" firstAttribute="trailing" secondItem="BQP-at-d9u" secondAttribute="trailing" id="Uef-P9-tya"/>
<constraint firstItem="vUN-kp-3ea" firstAttribute="bottom" secondItem="BQP-at-d9u" secondAttribute="bottom" id="dRt-R4-ATb"/>
<constraint firstItem="BQP-at-d9u" firstAttribute="leading" secondItem="vUN-kp-3ea" secondAttribute="leading" id="qrq-YF-Veu"/>
<constraint firstItem="BQP-at-d9u" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="centerY" id="y3J-8m-NMn"/>
<constraint firstItem="BQP-at-d9u" firstAttribute="top" secondItem="vUN-kp-3ea" secondAttribute="top" id="zzu-ki-ASh"/>
</constraints>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
<point key="canvasLocation" x="50" y="-184.5"/>
<point key="canvasLocation" x="50" y="-184"/>
</view>
</objects>
</document>
4 changes: 2 additions & 2 deletions StarsKit/Classes/UI/StarsPopViewController.xib
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14113" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand Down
11 changes: 6 additions & 5 deletions StarsKit/Classes/UI/Steps/StarsRateViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class StarsRateViewController: StepViewController {

@IBOutlet weak var ibCosmosView: CosmosView!
@IBOutlet weak var ibSmileyRateView: SmileyRateView!
@IBOutlet weak var ibStackView: UIStackView!

init(graphicContext: StarsKitGraphicContext, coordinator: RatingCoordinator) {
let nibName = "StarsRateViewController"
Expand All @@ -50,11 +51,11 @@ public class StarsRateViewController: StepViewController {
self.ibActionButton?.isEnabled = false
}

if StarsKit.shared.customImageMode {
self.ibCosmosView.removeFromSuperview()
self.ibSmileyRateView.images = self.graphicContext.customImages
if StarsKit.shared.customImageMode {
self.ibCosmosView.removeFromSuperview()
self.ibSmileyRateView.images = StarsKit.shared.graphicContext.customImages
self.ibSmileyRateView.computeView()
self.ibSmileyRateView.didFinishTouchingCosmos = { [weak self] rating in
self.ibSmileyRateView.didFinishTouching = { [weak self] rating in
self?.coordinator?.endRating(to: rating)
self?.ibActionButton?.isEnabled = true
}
Expand All @@ -75,7 +76,7 @@ public class StarsRateViewController: StepViewController {
}

@IBAction func didTapActionButton(_ sender: Any) {
self.coordinator?.validateRating(to: self.ibCosmosView.rating)
self.coordinator?.validateRating(to: StarsKit.shared.customImageMode ?self.ibSmileyRateView.rating : self.ibCosmosView.rating )
}

@IBAction public func didChooseDismissAction(_ sender: Any) {
Expand Down
Loading

0 comments on commit ce8a3e9

Please sign in to comment.