Skip to content

Commit

Permalink
I ran into problems with setting the photo center based on the conten…
Browse files Browse the repository at this point in the history
…tView center after residing the root view. I stripped out contentView because it was extraneous but still had the problem so I switched away from view.center because it's a trap and doesn't always represent the true view center.
  • Loading branch information
kgn committed Aug 31, 2012
1 parent 5cc4cf4 commit e01bc5a
Showing 1 changed file with 18 additions and 30 deletions.
48 changes: 18 additions & 30 deletions Deck/PhotoStackView.m
Expand Up @@ -15,7 +15,6 @@


@interface PhotoStackView() @interface PhotoStackView()


@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) NSArray *photoViews; @property (nonatomic, strong) NSArray *photoViews;


@end @end
Expand Down Expand Up @@ -57,23 +56,13 @@ -(void)setPhotoViews:(NSArray *)photoViews {
[self makeCrooked:view animated:NO]; [self makeCrooked:view animated:NO];
} }


[self.contentView addSubview:view]; [self insertSubview:view atIndex:0];
[self.contentView sendSubviewToBack:view];


} }


_photoViews = photoViews; _photoViews = photoViews;
} }


-(UIView *)contentView {

if(!_contentView) {
_contentView = [[UIView alloc] initWithFrame:self.bounds];
}

return _contentView;
}

-(UIImage *)borderImage { -(UIImage *)borderImage {
if(!_borderImage) { if(!_borderImage) {
_borderImage = [UIImage imageNamed:@"PhotoBorder"]; _borderImage = [UIImage imageNamed:@"PhotoBorder"];
Expand Down Expand Up @@ -147,7 +136,7 @@ -(void)returnToCenter:(UIView *)photo {


[UIView animateWithDuration:0.2 [UIView animateWithDuration:0.2
animations:^{ animations:^{
photo.center = CGPointMake(self.contentView.center.x, self.contentView.center.y); photo.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
}]; }];
} }


Expand All @@ -162,17 +151,18 @@ -(void)flickAway:(UIView *)photo withVelocity:(CGPoint)velocity {
} }
[self.delegate photoStackView:self willFlickAwayPhotoFromIndex:fromIndex toIndex:toIndex]; [self.delegate photoStackView:self willFlickAwayPhotoFromIndex:fromIndex toIndex:toIndex];
} }


CGFloat xPos = (velocity.x < 0) ? self.contentView.center.x-self.contentView.frame.size.width : self.contentView.center.y+self.contentView.frame.size.width; CGFloat width = CGRectGetWidth(self.bounds);
CGFloat xPos = (velocity.x < 0) ? CGRectGetMidX(self.bounds)-width : CGRectGetMidY(self.bounds)+width;


[UIView animateWithDuration:0.1 [UIView animateWithDuration:0.1
animations:^{ animations:^{
photo.center = CGPointMake(xPos, self.contentView.center.y); photo.center = CGPointMake(xPos, CGRectGetMidY(self.bounds));
} }
completion:^(BOOL finished){ completion:^(BOOL finished){


[self makeCrooked:photo animated:YES]; [self makeCrooked:photo animated:YES];
[self.contentView sendSubviewToBack:photo]; [self sendSubviewToBack:photo];
[self makeStraight:[self topPhoto] animated:YES]; [self makeStraight:[self topPhoto] animated:YES];
[self returnToCenter:photo]; [self returnToCenter:photo];


Expand Down Expand Up @@ -224,8 +214,8 @@ -(void)makeStraight:(UIView *)photo animated:(BOOL)animated {
-(void)photoPanned:(UIPanGestureRecognizer *)gesture { -(void)photoPanned:(UIPanGestureRecognizer *)gesture {


UIView *topPhoto = [self topPhoto]; UIView *topPhoto = [self topPhoto];
CGPoint velocity = [gesture velocityInView:self.contentView]; CGPoint velocity = [gesture velocityInView:self];
CGPoint translation = [gesture translationInView:self.contentView]; CGPoint translation = [gesture translationInView:self];


if(gesture.state == UIGestureRecognizerStateBegan) { if(gesture.state == UIGestureRecognizerStateBegan) {


Expand All @@ -243,7 +233,7 @@ -(void)photoPanned:(UIPanGestureRecognizer *)gesture {
CGFloat yPos = topPhoto.center.y + translation.y; CGFloat yPos = topPhoto.center.y + translation.y;


topPhoto.center = CGPointMake(xPos, yPos); topPhoto.center = CGPointMake(xPos, yPos);
[gesture setTranslation:CGPointMake(0, 0) inView:self.contentView]; [gesture setTranslation:CGPointMake(0, 0) inView:self];




} else if(gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled) { } else if(gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled) {
Expand Down Expand Up @@ -298,7 +288,7 @@ -(void)flipToNextPhoto{
-(void)goToPhotoAtIndex:(NSUInteger)index { -(void)goToPhotoAtIndex:(NSUInteger)index {
for (UIView *view in self.photoViews) { for (UIView *view in self.photoViews) {
if([self.photoViews indexOfObject:view] < index) { if([self.photoViews indexOfObject:view] < index) {
[self.contentView sendSubviewToBack:view]; [self sendSubviewToBack:view];
} }
} }
[self makeStraight:[self topPhoto] animated:NO]; [self makeStraight:[self topPhoto] animated:NO];
Expand All @@ -309,7 +299,7 @@ -(NSUInteger)indexOfTopPhoto {
} }


-(UIView *)topPhoto { -(UIView *)topPhoto {
return [self.contentView.subviews objectAtIndex:[self.contentView.subviews count]-1]; return [self.subviews objectAtIndex:[self.subviews count]-1];
} }


-(void)sendActionsForControlEvents:(UIControlEvents)controlEvents { -(void)sendActionsForControlEvents:(UIControlEvents)controlEvents {
Expand Down Expand Up @@ -378,12 +368,12 @@ -(void)reloadData {
[view addSubview:photoImageView]; [view addSubview:photoImageView];


view.tag = index; view.tag = index;
view.center = self.contentView.center; view.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));

[photoViewsMutable addObject:view]; [photoViewsMutable addObject:view];


} }

// Photo views are added to subview in the photoView setter // Photo views are added to subview in the photoView setter
self.photoViews = photoViewsMutable; photoViewsMutable = nil; self.photoViews = photoViewsMutable; photoViewsMutable = nil;
[self goToPhotoAtIndex:topPhotoIndex]; [self goToPhotoAtIndex:topPhotoIndex];
Expand All @@ -403,15 +393,14 @@ -(void)setup {
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(photoPanned:)]; UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(photoPanned:)];
[panGesture setMaximumNumberOfTouches:1]; [panGesture setMaximumNumberOfTouches:1];
panGesture.delegate = self; panGesture.delegate = self;
[self.contentView addGestureRecognizer:panGesture]; [self addGestureRecognizer:panGesture];


// Add Tap Gesture // Add Tap Gesture
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoTapped:)]; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoTapped:)];
[tapGesture setNumberOfTapsRequired:1]; [tapGesture setNumberOfTapsRequired:1];
tapGesture.delegate = self; tapGesture.delegate = self;
[self.contentView addGestureRecognizer:tapGesture]; [self addGestureRecognizer:tapGesture];


[self addSubview:self.contentView];
[self reloadData]; [self reloadData];
} }


Expand All @@ -433,7 +422,6 @@ -(void)dealloc {
[self setPhotoViews:nil]; [self setPhotoViews:nil];
[self setBorderImage:nil]; [self setBorderImage:nil];
[self setHighlightColor:nil]; [self setHighlightColor:nil];
[self setContentView:nil];
} }


@end @end

0 comments on commit e01bc5a

Please sign in to comment.