Skip to content

Commit

Permalink
Merge pull request #2 from andygeers/handle_rotations
Browse files Browse the repository at this point in the history
Handle rotations
  • Loading branch information
ddoria921 committed Mar 5, 2015
2 parents f931d31 + aef32ec commit 51e1b5d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 21 deletions.
51 changes: 42 additions & 9 deletions Coach Marks/DDBubble.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,28 @@ -(id)initWithFrame:(CGRect)frame title:(NSString*)title description:(NSString*)d
[self setFrame:[self calculateFrameWithFont:font]];
[self fixFrameIfOutOfBounds];

// Make it pass touch events through to the DDCoachMarksView
[self setUserInteractionEnabled:NO];

// calculate and position text
CGSize offsets = [self offsets];
float actualXPosition = offsets.width+PADDING*1.5;
float actualYPosition = offsets.height+PADDING*1.25;
float actualWidth = self.frame.size.width-offsets.width-PADDING*3;
float actualHeight = self.frame.size.height-offsets.height-PADDING*2.5;
float actualWidth = self.frame.size.width-actualXPosition - PADDING*1.5;
float actualHeight = self.frame.size.height - actualYPosition - PADDING*1.2;

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(actualXPosition, actualYPosition, actualWidth, actualHeight)];
[titleLabel setFont:font];
[titleLabel setTextColor:TEXT_COLOR];
[titleLabel setAlpha:0.9];
[titleLabel setFont:font];
[titleLabel setText:title];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
[titleLabel setNumberOfLines:0];
[titleLabel setUserInteractionEnabled:NO];
[self addSubview:titleLabel];


[self setNeedsDisplay];
return self;
}
Expand All @@ -95,7 +99,7 @@ - (void)fixFrameIfOutOfBounds

CGRect window = [[[UIApplication sharedApplication] keyWindow] frame];
const float xBounds = window.size.width; // 320;
// const float yBounds = 568;
const float yBounds = window.size.height;

float x = self.frame.origin.x;
float y = self.frame.origin.y;
Expand All @@ -111,10 +115,30 @@ - (void)fixFrameIfOutOfBounds
}
// check for left most bound
if (x < 0) {
_arrowOffset = x - padding;
if (_arrowOffset == 0) {
_arrowOffset = x - padding;
}
x = 0;
}

// If the content pushes us off the vertical bounds we might have to be more drastic
// and flip the arrow direction
if ((self.arrowPosition == CRArrowPositionTop) && (y + height > yBounds)) {
self.arrowPosition = CRArrowPositionBottom;

// Restart the entire process
CGRect flippedFrame = [self calculateFrameWithFont:[self font]];
y = flippedFrame.origin.y;
height = flippedFrame.size.height;
} else if ((self.arrowPosition == CRArrowPositionBottom) && (y < 0)) {
self.arrowPosition = CRArrowPositionTop;

// Restart the entire process
CGRect flippedFrame = [self calculateFrameWithFont:[self font]];
y = flippedFrame.origin.y;
height = flippedFrame.size.height;
}

[self setFrame:CGRectMake(x, y, width, height)];
}

Expand All @@ -126,29 +150,38 @@ -(CGRect)calculateFrameWithFont:(UIFont*)font

CGSize size = [self sizeWithFont:font];

float widthDelta = 0, heightDelta = 0;

if(self.arrowPosition==CRArrowPositionLeft||self.arrowPosition==CRArrowPositionRight)
{
y+=self.attachedFrame.size.height/2-size.height/2;
x+=(self.arrowPosition==CRArrowPositionLeft)? ARROW_SPACE+self.attachedFrame.size.width : -(ARROW_SPACE*2+size.width);
widthDelta = ARROW_SIZE;

}else if(self.arrowPosition==CRArrowPositionTop||self.arrowPosition==CRArrowPositionBottom)
{
x+=self.attachedFrame.size.width/2-size.width/2;
y+=(self.arrowPosition==CRArrowPositionTop)? ARROW_SPACE+self.attachedFrame.size.height : -(ARROW_SPACE*2+size.height);
heightDelta = ARROW_SIZE;
}

CGSize offsets = [self offsets];
return CGRectMake(x, y, size.width+offsets.width, size.height+offsets.height);
return CGRectMake(x, y, size.width+widthDelta, size.height+heightDelta);
}

-(CGSize)sizeWithFont:(UIFont*)font
{
// Calcultation of the bubble size
// size of bubble title determined by the strings attributes
CGSize offset = [self offsets];
CGRect window = [[[UIApplication sharedApplication] keyWindow] frame];

CGSize result = [_title sizeWithFont:font constrainedToSize:CGSizeMake(window.size.width - offset.width - (PADDING*4), FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
float widthDelta = 0;
if(self.arrowPosition==CRArrowPositionLeft||self.arrowPosition==CRArrowPositionRight)
{
// Make space for an arrow on one side
widthDelta = ARROW_SIZE;
}

CGSize result = [_title sizeWithFont:font constrainedToSize:CGSizeMake(window.size.width - widthDelta - (PADDING*3), FLT_MAX) lineBreakMode:NSLineBreakByWordWrapping];

return CGSizeMake(result.width + (PADDING*3), result.height + (PADDING*2.5));
}
Expand Down
6 changes: 3 additions & 3 deletions Coach Marks/DDCircleView.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ - (void)userTap:(UITapGestureRecognizer *)recognizer {

- (void)swipeInFrame:(CGRect)frame
{
[self centerYPositioninView:self inFrame:frame];
[self centerInView:self inFrame:frame];
[self animateSwipe];
}

Expand All @@ -54,7 +54,7 @@ - (void)animateSwipe
self.transform = CGAffineTransformConcat(translateRight, scale);
}
self.alpha = 0.0f;
[UIView animateKeyframesWithDuration:0.6 delay:0.3 options:0
[UIView animateWithDuration:0.6 delay:0.3 options:0
animations:^{
// Fade In
if (self.swipeDirection == kCircleSwipeLeftToRight) {
Expand Down Expand Up @@ -101,7 +101,7 @@ - (void)centerYPositioninView:(UIView *)view inFrame:(CGRect)frame
- (void)centerXPositioninView:(UIView *)view inFrame:(CGRect)frame
{
CGFloat centerX = frame.origin.x + CGRectGetWidth(frame)/2;
CGFloat offsetX = CGRectGetWidth(view.frame)/2;
CGFloat offsetX = 260/2;

CGFloat newX = centerX - offsetX;
view.frame = CGRectMake(newX, view.frame.origin.y, 40, 40);
Expand Down
18 changes: 9 additions & 9 deletions Coach Marks/DDCoachMarksView.m
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,16 @@ - (void)goToCoachMarkIndexed:(NSUInteger)index {

// Current index
markIndex = index;

// Coach mark definition
NSDictionary *markDef = [self.coachMarks objectAtIndex:index];
CGRect markRect = [[markDef objectForKey:@"rect"] CGRectValue];
NSString *shape = [markDef objectForKey:@"shape"];

// Delegate (coachMarksView:willNavigateTo:atIndex:)
if ([self.delegate respondsToSelector:@selector(coachMarksView:willNavigateToIndex:)]) {
[self.delegate coachMarksView:self willNavigateToIndex:markIndex];
}

// Coach mark definition
NSDictionary *markDef = [self.coachMarks objectAtIndex:index];
CGRect markRect = [[markDef objectForKey:@"rect"] CGRectValue];
NSString *shape = [markDef objectForKey:@"shape"];

if (self.useBubbles) {
[self animateNextBubble];
Expand Down Expand Up @@ -230,7 +230,7 @@ - (void)showSwipeGesture

// create an animating circle and animate it
if (shouldAnimateSwipe) {
self.animatingCircle = [[DDCircleView alloc] initWithFrame:CGRectZero];
self.animatingCircle = [[DDCircleView alloc] initWithFrame:self.frame];

if (![self.subviews containsObject:self.animatingCircle]) {
[self addSubview:self.animatingCircle];
Expand All @@ -255,7 +255,7 @@ - (void)animateNextBubble

// remove previous bubble
if (self.bubble) {
[UIView animateKeyframesWithDuration:0.3 delay:0.0 options:0
[UIView animateWithDuration:0.3 delay:0.0 options:0
animations:^{
self.bubble.alpha = 0.0;
} completion:nil];
Expand All @@ -277,7 +277,7 @@ - (void)animateNextBubble
[self addSubview:self.bubble];

// fade in & bounce animation
[UIView animateKeyframesWithDuration:0.8 delay:0.3 options:0
[UIView animateWithDuration:0.8 delay:0.3 options:0
animations:^{
self.bubble.alpha = 1.0;
[self.bubble animate];
Expand All @@ -297,7 +297,7 @@ - (void)cleanup {
__weak DDCoachMarksView *weakSelf = self;

// animate & remove from super view
[UIView animateKeyframesWithDuration:0.6 delay:0.3 options:0
[UIView animateWithDuration:0.6 delay:0.3 options:0
animations:^{
self.alpha = 0.0f;
self.animatingCircle.alpha = 0.0f;
Expand Down

0 comments on commit 51e1b5d

Please sign in to comment.