Skip to content
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

Add caging area #3

Merged
merged 5 commits into from
Oct 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions UIView+draggable/UIView+draggable.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@
*/
@property (nonatomic) UIPanGestureRecognizer *panGesture;

/**
A caging area such that the view can not be moved outside
of this frame.

If @c cagingArea is not @c CGRectZero, and @c cagingArea does not contain the
view's frame then this does nothing (ie. if the bounds of the view extend the
bounds of @c cagingArea).

Optional. If not set, defaults to @c CGRectZero, which will result
in no caging behavior.
*/
@property (nonatomic) CGRect cagingArea;


/**-----------------------------------------------------------------------------
* @name UIView+draggable Methods
* -----------------------------------------------------------------------------
Expand Down
55 changes: 52 additions & 3 deletions UIView+draggable/UIView+draggable.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,63 @@ - (UIPanGestureRecognizer*)panGesture
return objc_getAssociatedObject(self, @selector(panGesture));
}

- (void)setCagingArea:(CGRect)cagingArea
{
if (CGRectEqualToRect(cagingArea, CGRectZero) ||
CGRectContainsRect(cagingArea, self.frame)) {
NSValue *cagingAreaValue = [NSValue valueWithCGRect:cagingArea];
objc_setAssociatedObject(self,
@selector(cagingArea),
cagingAreaValue,
OBJC_ASSOCIATION_RETAIN);
}
}

- (CGRect)cagingArea
{
NSValue *cagingAreaValue = objc_getAssociatedObject(self,
@selector(cagingArea));

return [cagingAreaValue CGRectValue];
}

- (void)handlePan:(UIPanGestureRecognizer*)sender
{
[self adjustAnchorPointForGestureRecognizer:sender];

CGPoint translation = [sender translationInView:[self superview]];
[self setCenter:CGPointMake([self center].x + translation.x, [self center].y + translation.y)];

[sender setTranslation:(CGPoint){0, 0} inView:[self superview]];

CGFloat newXOrigin = CGRectGetMinX(self.frame) + translation.x;
CGFloat newYOrigin = CGRectGetMinY(self.frame) + translation.y;

CGRect cagingArea = self.cagingArea;

CGFloat cagingAreaOriginX = CGRectGetMinX(cagingArea);
CGFloat cagingAreaOriginY = CGRectGetMinY(cagingArea);

CGFloat cagingAreaRightSide = cagingAreaOriginX + CGRectGetWidth(cagingArea);
CGFloat cagingAreaBottomSide = cagingAreaOriginY + CGRectGetHeight(cagingArea);

if (!CGRectEqualToRect(cagingArea, CGRectZero)) {

// Check to make sure the view is still within the caging area
if (newXOrigin <= cagingAreaOriginX ||
newYOrigin <= cagingAreaOriginY ||
newXOrigin + CGRectGetWidth(self.frame) >= cagingAreaRightSide ||
newYOrigin + CGRectGetHeight(self.frame) >= cagingAreaBottomSide) {

// Don't move
newXOrigin = CGRectGetMinX(self.frame);
newYOrigin = CGRectGetMinY(self.frame);
}
}

[self setFrame:CGRectMake(newXOrigin,
newYOrigin,
CGRectGetWidth(self.frame),
CGRectGetHeight(self.frame))];

[sender setTranslation:(CGPoint){0, 0} inView:[self superview]];
}

- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
Expand Down
24 changes: 16 additions & 8 deletions UIViewDraggableDemo/UIViewDraggableDemo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5053" systemVersion="13C64" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6245" systemVersion="14A389" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
<deployment defaultVersion="1808" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6238"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -18,39 +19,46 @@
<subviews>
<view alpha="0.69999999999999973" contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="68k-Ly-nPK">
<rect key="frame" x="35" y="35" width="200" height="200"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Drag me around" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="KHY-TF-Ilz">
<rect key="frame" x="20" y="89" width="160" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="Futura-Medium" family="Futura" pointSize="17"/>
<color key="textColor" red="0.18039215689999999" green="0.3411764706" blue="0.5568627451" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Set caging area to screen" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="cXz-Hc-A44">
<rect key="frame" x="39" y="46" width="123" height="21"/>
<fontDescription key="fontDescription" name="Futura-Medium" family="Futura" pointSize="9"/>
<color key="textColor" red="0.18039215689999999" green="0.3411764706" blue="0.5568627451" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="1hG-MN-CcP">
<rect key="frame" x="76" y="13" width="51" height="31"/>
<color key="onTintColor" red="0.18039215689999999" green="0.3411764706" blue="0.5568627451" alpha="1" colorSpace="calibratedRGB"/>
<connections>
<action selector="didToggleCagingAreaSwitch:" destination="vXZ-lx-hvc" eventType="valueChanged" id="lep-oR-NiT"/>
</connections>
</switch>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
<view alpha="0.69999998807907104" contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="s6E-lo-py3">
<rect key="frame" x="86" y="155" width="200" height="200"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Drag me around" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="xb1-5O-BBj">
<rect key="frame" x="20" y="89" width="160" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="Futura-Medium" family="Futura" pointSize="17"/>
<color key="textColor" red="0.18039215689999999" green="0.3411764706" blue="0.5568627451" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Enable dragging" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Mov-Rb-b89">
<rect key="frame" x="20" y="177" width="160" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="Futura-Medium" family="Futura" pointSize="9"/>
<color key="textColor" red="0.18039215689999999" green="0.3411764706" blue="0.5568627451" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="AsR-NA-94F">
<rect key="frame" x="76" y="149" width="51" height="31"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="onTintColor" red="0.18039215689999999" green="0.3411764706" blue="0.5568627451" alpha="1" colorSpace="calibratedRGB"/>
<connections>
<action selector="actionSwitch:" destination="vXZ-lx-hvc" eventType="valueChanged" id="XbP-hg-onk"/>
Expand Down
12 changes: 12 additions & 0 deletions UIViewDraggableDemo/UIViewDraggableDemo/FPViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,17 @@ - (IBAction)actionSwitch:(UISwitch*)sender
[obj setDraggable:sender.isOn];
}];
}
- (IBAction)didToggleCagingAreaSwitch:(UISwitch *)sender
{
CGRect cagingArea = CGRectZero;

if ([sender isOn]) {
cagingArea = self.view.frame;
}

[self.draggableViews enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL *stop) {
obj.cagingArea = cagingArea;
}];
}

@end