Skip to content

add highlightBackgoundColor #14

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

Open
wants to merge 5 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
Binary file modified .DS_Store
Binary file not shown.
6 changes: 3 additions & 3 deletions Demo/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "xcode",
"version" : 1
}
}
}
22 changes: 22 additions & 0 deletions Demo/Assets.xcassets/登录-底图 2.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "登录-底图 [email protected]",
"idiom" : "universal",
"scale" : "2x"
},
{
"filename" : "登录-底图 [email protected]",
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
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.
27 changes: 22 additions & 5 deletions Demo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="23504" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina6_12" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23506"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="Demo" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<rect key="frame" x="0.0" y="0.0" width="393" height="852"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<subviews>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="登录-底图 2" translatesAutoresizingMaskIntoConstraints="NO" id="aQj-eV-BgG">
<rect key="frame" x="0.0" y="0.0" width="393" height="852"/>
</imageView>
</subviews>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="bottom" secondItem="aQj-eV-BgG" secondAttribute="bottom" id="FHR-Sn-UDd"/>
<constraint firstItem="aQj-eV-BgG" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" id="SgH-Zx-Gfm"/>
<constraint firstItem="aQj-eV-BgG" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" id="icX-Op-EoN"/>
<constraint firstItem="aQj-eV-BgG" firstAttribute="trailing" secondItem="6Tk-OE-BBY" secondAttribute="trailing" id="qE2-ND-3QW"/>
</constraints>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="93.89312977099236" y="-28.169014084507044"/>
</scene>
</scenes>
<resources>
<image name="登录-底图 2" width="375" height="812"/>
</resources>
</document>
20 changes: 11 additions & 9 deletions Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ViewController: UIViewController {
// default item view
let pinCodeInputView: PinCodeInputView<ItemView> = .init(
digit: 6,
itemSpacing: 8,
itemSpacing: 10,
itemFactory: {
return ItemView()
},
Expand Down Expand Up @@ -65,22 +65,24 @@ class ViewController: UIViewController {
titleLabel.font = UIFont.systemFont(ofSize: 24, weight: .bold)
titleLabel.textColor = UIColor.lightText
titleLabel.frame = CGRect(x: 0, y: 0, width: view.bounds.width - 56, height: 60)
titleLabel.center = CGPoint(x: view.center.x, y: view.center.y - 94)
titleLabel.center = CGPoint(x: view.center.x, y: view.center.y - 124)

pinCodeInputView.frame = CGRect(x: 0, y: 0, width: view.bounds.width - 56, height: 80)
pinCodeInputView.center = view.center
pinCodeInputView.center = CGPoint(x: view.center.x, y: view.center.y - 30)
pinCodeInputView.set(changeTextHandler: { text in
print(text)
})
pinCodeInputView.set(
appearance: .init(
itemSize: CGSize(width: 44, height: 68),
font: .systemFont(ofSize: 28, weight: .bold),
textColor: .white,
backgroundColor: UIColor.white.withAlphaComponent(0.3),
itemSize: CGSize(width: 48, height: 48),
font: .systemFont(ofSize: 20, weight: .bold),
textColor: .black,
backgroundColor: UIColor.white.withAlphaComponent(0.8),
highlightBackgroundColor: UIColor(red: 186/255.0, green: 212/255.0, blue: 255/255.0, alpha: 0.8),
cursorColor: UIColor(red: 69/255, green: 108/255, blue: 1, alpha: 1),
cornerRadius: 8,
borderColor: UIColor.red
cornerRadius: 4,
highlightBorderColor: UIColor(red: 38/255.0, green: 86/255.0, blue: 235/255.0, alpha: 1),
borderColor:.white
)
)

Expand Down
2 changes: 1 addition & 1 deletion PinCodeInputView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Pod::Spec.new do |s|

s.name = "PinCodeInputView"
s.version = "1.0.0"
s.version = "1.1.0"
s.summary = "TextView for entering pin code. "

s.description = <<-DESC
Expand Down
4 changes: 2 additions & 2 deletions PinCodeInputView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 64T4PJ8WCZ;
DEVELOPMENT_TEAM = 2286X5X7D9;
INFOPLIST_FILE = Demo/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand All @@ -521,7 +521,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 64T4PJ8WCZ;
DEVELOPMENT_TEAM = 2286X5X7D9;
INFOPLIST_FILE = Demo/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
Expand Down
7 changes: 7 additions & 0 deletions PinCodeInputView/ItemAppearance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,32 @@ public struct ItemAppearance {
public let font: UIFont
public let textColor: UIColor
public let backgroundColor: UIColor
public let highlightBackgroundColor: UIColor
public let cursorColor: UIColor
public let cornerRadius: CGFloat
public let borderColor: UIColor
public let highlightBorderColor: UIColor


public init(
itemSize: CGSize,
font: UIFont,
textColor: UIColor,
backgroundColor: UIColor,
highlightBackgroundColor: UIColor,
cursorColor: UIColor,
cornerRadius: CGFloat,
highlightBorderColor: UIColor = UIColor.clear,
borderColor: UIColor = UIColor.clear) {

self.itemSize = itemSize
self.font = font
self.textColor = textColor
self.backgroundColor = backgroundColor
self.highlightBackgroundColor = highlightBackgroundColor
self.cursorColor = cursorColor
self.cornerRadius = cornerRadius
self.borderColor = borderColor
self.highlightBorderColor = highlightBorderColor
}
}
60 changes: 38 additions & 22 deletions PinCodeInputView/ItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ public class ItemView: UIView, ItemType {
didSet {
cursor.isHidden = isHiddenCursor
if (isHiddenCursor) {
self.layer.borderWidth = 0
self.backgroundColor = _appearance?.backgroundColor
self.layer.borderColor = _appearance?.borderColor.cgColor
self.layer.borderWidth = 1
} else {
self.backgroundColor = _appearance?.highlightBackgroundColor
self.layer.borderColor = _appearance?.highlightBorderColor.cgColor
self.layer.borderWidth = 1
}
}
Expand All @@ -50,27 +54,36 @@ public class ItemView: UIView, ItemType {
label.isUserInteractionEnabled = false

cursor.isHidden = true

UIView.animateKeyframes(
withDuration: 1.6,
delay: 0.8,
options: [.repeat],
animations: {
UIView.addKeyframe(
withRelativeStartTime: 0,
relativeDuration: 0.2,
animations: {
self.cursor.alpha = 0
})
UIView.addKeyframe(
withRelativeStartTime: 0.8,
relativeDuration: 0.2,
animations: {
self.cursor.alpha = 1
})
},
completion: nil
)
NotificationCenter.default.addObserver(self, selector: #selector(becomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(enterBack), name: UIApplication.didEnterBackgroundNotification, object: nil)
becomeActive()

}

/// 闪烁动画
fileprivate var opacityAnimation: CABasicAnimation = {
let opacityAnimation = CABasicAnimation.init(keyPath: "opacity")
opacityAnimation.fromValue = 1.0
opacityAnimation.toValue = 0.0
opacityAnimation.duration = 0.9
opacityAnimation.repeatCount = HUGE
opacityAnimation.isRemovedOnCompletion = true
opacityAnimation.fillMode = .forwards
opacityAnimation.timingFunction = CAMediaTimingFunction.init(name: .linear)
return opacityAnimation
}()


/// 去后台
@objc fileprivate func enterBack() {
// 移除动画
cursor.layer.removeAnimation(forKey: "kOpacityAnimation")
}

/// 回前台
@objc fileprivate func becomeActive() {
// 重新添加动画
cursor.layer.add(opacityAnimation, forKey: "kOpacityAnimation")
}

required init?(coder aDecoder: NSCoder) {
Expand All @@ -93,7 +106,10 @@ public class ItemView: UIView, ItemType {
)
}

private var _appearance: ItemAppearance?

public func set(appearance: ItemAppearance) {
_appearance = appearance
bounds.size = appearance.itemSize
label.font = appearance.font
label.textColor = appearance.textColor
Expand Down
43 changes: 42 additions & 1 deletion PinCodeInputView/PinCodeInputView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public class PinCodeInputView<T: UIView & ItemType>: UIControl, UITextInputTrait
public var hasText: Bool {
return !(text.isEmpty)
}

public var currentFocusIndex: Int {
if isFilled { return 0 }
return text.count
}

override public var intrinsicContentSize: CGSize {
return stackView.bounds.size
Expand Down Expand Up @@ -86,6 +91,42 @@ public class PinCodeInputView<T: UIView & ItemType>: UIControl, UITextInputTrait
stackView.spacing = itemSpacing
stackView.axis = .horizontal
stackView.distribution = .fillEqually
setupGestures()
}


private func setupGestures() {
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
self.addGestureRecognizer(longPressGesture)
}

// MARK: - Long Press Handler

@objc private func handleLongPress(_ gesture: UILongPressGestureRecognizer) {
if gesture.state == .began {
becomeFirstResponder()
// 显示粘贴菜单

let menu = UIMenuController.shared
menu.menuItems = [UIMenuItem(title: "粘贴", action: #selector(pasteText))]
let focusView = self.items[currentFocusIndex]
let targetFrame = focusView.convert(focusView.bounds, to: self)
menu.setTargetRect(targetFrame, in: self)
menu.setMenuVisible(true, animated: true)
}
}

@objc private func pasteText() {
if let pasteString = UIPasteboard.general.string {
set(text: pasteString)
}
}

public override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(pasteText) {
return UIPasteboard.general.string != nil
}
return false
}

required init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -124,7 +165,7 @@ public class PinCodeInputView<T: UIView & ItemType>: UIControl, UITextInputTrait
public func set(appearance: ItemAppearance) {
self.appearance = appearance
if autoResizes {
self.appearance = ItemAppearance(itemSize: CGSize(width: (self.bounds.width - (self.itemSpacing * CGFloat(self.digit))) / CGFloat(self.digit), height: appearance.itemSize.height), font: appearance.font, textColor: appearance.textColor, backgroundColor: appearance.backgroundColor, cursorColor: appearance.cursorColor, cornerRadius: appearance.cornerRadius, borderColor: appearance.borderColor)
self.appearance = ItemAppearance(itemSize: CGSize(width: (self.bounds.width - (self.itemSpacing * CGFloat(self.digit))) / CGFloat(self.digit), height: appearance.itemSize.height), font: appearance.font, textColor: appearance.textColor, backgroundColor: appearance.backgroundColor, highlightBackgroundColor: appearance.highlightBackgroundColor, cursorColor: appearance.cursorColor, cornerRadius: appearance.cornerRadius, highlightBorderColor: appearance.highlightBorderColor, borderColor: appearance.borderColor)
}
items.forEach { $0.itemView.set(appearance: appearance) }
}
Expand Down