Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Why The Bits
typedef enum {
BKSlideDirectionRight = 1 << 0,
BKSlideDirectionLeft = 1 << 1,
BKSlideDirectionUp = 1 << 2,
BKSlideDirectionDown = 1 << 3,
BKSlideDirectionVertical = BKSlideDirectionUp | BKSlideDirectionDown,
BKSlideDirectionHorizontal = BKSlideDirectionLeft | BKSlideDirectionRight
} BKSlideDirection;
BK_EXTERN BKSlideDirection BKSlideOrientation(BKSlideDirection direction);
BK_EXTERN BKSlideDirection BKSlideOtherDirection(BKSlideDirection direction);
BK_EXTERN BOOL BKSlideIsDirection(BKSlideDirection check, BKSlideDirection direction);
BK_EXTERN CGPoint BKSlideOffscreenCenterPoint(CGRect enclosingFrame, CGRect frame, BKSlideDirection direction);
BK_EXTERN CGPoint CGPointCenterOfRect(CGRect frame);
.m file
BK_EXTERN BOOL BKSlideIsDirection(BKSlideDirection check, BKSlideDirection direction) {
BOOL value = (check | direction) == check;
return value;
}
BK_EXTERN BKSlideDirection BKSlideOrientation(BKSlideDirection direction) {
if ((direction | BKSlideDirectionHorizontal) == BKSlideDirectionHorizontal) {
return BKSlideDirectionHorizontal;
} else {
return BKSlideDirectionVertical;
}
}
BK_EXTERN BKSlideDirection BKSlideOtherDirection(BKSlideDirection direction) {
BKSlideDirection orientation = BKSlideOrientation(direction);
return (direction ^ orientation);
}
BK_EXTERN CGPoint CGPointCenterOfRect(CGRect frame) {
return CGPointMake(frame.origin.x + (frame.size.width / 2),
frame.origin.y + (frame.size.height / 2));
}
BK_EXTERN CGPoint BKSlideOffscreenCenterPoint(CGRect enclosingFrame, CGRect frame, BKSlideDirection direction) {
// In theory this might handle diagonals too, but the mask would need to be altered.
CGPoint offscreenCenter = CGPointCenterOfRect(frame);
if (BKSlideIsDirection(BKSlideDirectionVertical, direction)) {
CGFloat shift = frame.size.height / 2;
if (BKSlideIsDirection(BKSlideDirectionUp, direction)) {
offscreenCenter.y = enclosingFrame.origin.y - shift;
} else if (BKSlideIsDirection(BKSlideDirectionDown, direction)) {
offscreenCenter.y = enclosingFrame.size.height + shift;
}
}
if (BKSlideIsDirection(BKSlideDirectionHorizontal, direction)) {
CGFloat shift = frame.size.width / 2;
if (BKSlideIsDirection(BKSlideDirectionRight, direction)) {
offscreenCenter.x = enclosingFrame.size.width + shift;
} else if (BKSlideIsDirection(BKSlideDirectionLeft, direction)) {
offscreenCenter.y = enclosingFrame.origin.x - shift;
}
}
return offscreenCenter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment