Skip to content

Commit 9c26925

Browse files
committed
Ver 0.4.0
1 parent c0cd6a8 commit 9c26925

File tree

2 files changed

+56
-96
lines changed

2 files changed

+56
-96
lines changed

RCStickerView.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'RCStickerView'
11-
s.version = '0.3.0'
11+
s.version = '0.4.0'
1212
s.summary = 'Add label, image, other custom view as a sticker view.'
1313

1414
# This description is used to generate tags and improve search results.

RCStickerView/Classes/RCStickerView.swift

Lines changed: 55 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,49 @@ open class RCStickerView: UIView {
446446
self.contentView.layer.borderColor = outlineBorderColor.cgColor
447447
}
448448
}
449+
450+
func layoutInsideBounds(in view: UIView) {
451+
let frameInSuperview = view.convert(view.bounds, to: self.superview)
452+
453+
var x = self.center.x
454+
var y = self.center.y
455+
456+
var topPadding: CGFloat = 0
457+
var leftPadding: CGFloat = 0
458+
var rightPadding: CGFloat = 0
459+
var bottomPadding: CGFloat = 0
460+
461+
if positionVisibilityMap[.topLeft]! || positionVisibilityMap[.topRight]! {
462+
topPadding = _handleSize - frameInSuperview.origin.y
463+
}
464+
if positionVisibilityMap[.topLeft]! || positionVisibilityMap[.bottomLeft]! {
465+
leftPadding = _handleSize - frameInSuperview.origin.x
466+
}
467+
if positionVisibilityMap[.bottomRight]! || positionVisibilityMap[.topRight]! {
468+
rightPadding = _handleSize + frameInSuperview.origin.x
469+
}
470+
if positionVisibilityMap[.bottomRight]! || positionVisibilityMap[.bottomLeft]! {
471+
bottomPadding = _handleSize + frameInSuperview.origin.y
472+
}
473+
474+
if x < frame.width / 2 - leftPadding {
475+
x = frame.width / 2 - leftPadding
476+
}
477+
478+
if y < frame.height / 2 - topPadding {
479+
y = frame.height / 2 - topPadding
480+
}
481+
482+
if x > frameInSuperview.width - frame.width / 2 + rightPadding {
483+
x = frameInSuperview.width - frame.width / 2 + rightPadding
484+
}
485+
486+
if y > frameInSuperview.height - frame.height / 2 + bottomPadding {
487+
y = frameInSuperview.height - frame.height / 2 + bottomPadding
488+
}
489+
490+
self.center = CGPoint(x: x, y: y)
491+
}
449492
}
450493

451494
private extension RCStickerView {
@@ -695,15 +738,25 @@ private extension RCStickerView {
695738
case .ended:
696739
self.transform = .identity
697740
UIView.animate(withDuration: 0.35) {
698-
self.onMoveWhileZooming(recognizer)
741+
switch self.zoomMode {
742+
case .free:
743+
break
744+
case .insideSuperview:
745+
let view = self.superview ?? self.window
746+
if let view = view {
747+
self.layoutInsideBounds(in: view)
748+
}
749+
case .inside(let view):
750+
self.layoutInsideBounds(in: view)
751+
}
699752
}
700753
self.delegate?.stickerViewDidEndZooming?(self)
701754
default:
702755
return
703756
}
704757
}
705758

706-
private func layoutDashedLineBorder() {
759+
func layoutDashedLineBorder() {
707760
self.dashedLineBorder.bounds = self.contentView.bounds
708761
self.dashedLineBorder.position = CGPoint(x: contentView.frame.width / 2, y: contentView.frame.height / 2)
709762
let path = UIBezierPath.init(roundedRect: contentView.bounds, cornerRadius: 0)
@@ -728,99 +781,6 @@ private extension RCStickerView {
728781

729782
return expectedBounds
730783
}
731-
732-
private func onMoveWhileZooming(_ recognizer: UIPinchGestureRecognizer) {
733-
let beginningCenter = self.center
734-
var touchLocation: CGPoint
735-
var x: CGFloat
736-
var y: CGFloat
737-
738-
switch zoomMode {
739-
case .free:
740-
touchLocation = recognizer.location(in: self.superview)
741-
x = beginningCenter.x + touchLocation.x - touchLocation.x
742-
y = beginningCenter.y + touchLocation.y - touchLocation.y
743-
case .insideSuperview:
744-
touchLocation = recognizer.location(in: self.superview)
745-
x = beginningCenter.x + touchLocation.x
746-
y = beginningCenter.y + touchLocation.y
747-
748-
var topPadding: CGFloat = 0
749-
var leftPadding: CGFloat = 0
750-
var rightPadding: CGFloat = 0
751-
var bottomPadding: CGFloat = 0
752-
if positionVisibilityMap[.topLeft]! || positionVisibilityMap[.topRight]! {
753-
topPadding = _handleSize
754-
}
755-
if positionVisibilityMap[.topLeft]! || positionVisibilityMap[.bottomLeft]! {
756-
leftPadding = _handleSize
757-
}
758-
if positionVisibilityMap[.bottomRight]! || positionVisibilityMap[.topRight]! {
759-
rightPadding = _handleSize
760-
}
761-
if positionVisibilityMap[.bottomRight]! || positionVisibilityMap[.bottomLeft]! {
762-
bottomPadding = _handleSize
763-
}
764-
765-
if x < frame.width / 2 - leftPadding {
766-
x = frame.width / 2 - leftPadding
767-
}
768-
769-
if y < frame.height / 2 - topPadding {
770-
y = frame.height / 2 - topPadding
771-
}
772-
773-
let superview = self.superview ?? self.window
774-
if let superview = superview {
775-
if x > superview.frame.width - frame.width / 2 + rightPadding {
776-
x = superview.frame.width - frame.width / 2 + rightPadding
777-
}
778-
779-
if y > superview.frame.height - frame.height / 2 + bottomPadding {
780-
y = superview.frame.height - frame.height / 2 + bottomPadding
781-
}
782-
}
783-
case .inside(let view):
784-
touchLocation = recognizer.location(in: view)
785-
x = beginningCenter.x + touchLocation.x
786-
y = beginningCenter.y + touchLocation.y
787-
788-
var topPadding: CGFloat = 0
789-
var leftPadding: CGFloat = 0
790-
var rightPadding: CGFloat = 0
791-
var bottomPadding: CGFloat = 0
792-
if positionVisibilityMap[.topLeft]! || positionVisibilityMap[.topRight]! {
793-
topPadding = _handleSize - view.frame.origin.y
794-
}
795-
if positionVisibilityMap[.topLeft]! || positionVisibilityMap[.bottomLeft]! {
796-
leftPadding = _handleSize - view.frame.origin.x
797-
}
798-
if positionVisibilityMap[.bottomRight]! || positionVisibilityMap[.topRight]! {
799-
rightPadding = _handleSize + view.frame.origin.x
800-
}
801-
if positionVisibilityMap[.bottomRight]! || positionVisibilityMap[.bottomLeft]! {
802-
bottomPadding = _handleSize + view.frame.origin.y
803-
}
804-
805-
if x < frame.width / 2 - leftPadding {
806-
x = frame.width / 2 - leftPadding
807-
}
808-
809-
if y < frame.height / 2 - topPadding {
810-
y = frame.height / 2 - topPadding
811-
}
812-
813-
if x > view.frame.width - frame.width / 2 + rightPadding {
814-
x = view.frame.width - frame.width / 2 + rightPadding
815-
}
816-
817-
if y > view.frame.height - frame.height / 2 + bottomPadding {
818-
y = view.frame.height - frame.height / 2 + bottomPadding
819-
}
820-
}
821-
822-
self.center = CGPoint(x: x, y: y)
823-
}
824784
}
825785

826786
extension RCStickerView: UIGestureRecognizerDelegate {

0 commit comments

Comments
 (0)