0

I'm working on an app where 1 ViewController has a UISegmentControl with these behaviours:

Segment 1 (if it's not already in position):

  • Fade in items
  • Slide segment control down

Segment 2 (if it's not already in position):

  • Fade out items
  • Slide segment control up

Segment 3 (if it's not already in position):

  • Not implemented yet, but similar to Segment 2

To achieve this, I'm using this code:

- (IBAction)segmentControlAction:(id)sender {
// Change which container will be visible
int selectedIndex = self.overviewSegmentControl.selectedSegmentIndex;

if (selectedIndex == 0) {
    // Show details and hide reviews & related
    self.detailContainer.hidden = NO;
    self.relatedContainer.hidden = YES;

    // SHOW THE DETAILS
    [self showDetails];
} else if (selectedIndex == 1) {
    // Show Reviews and hide details & related
    self.reviewsContainer.hidden = NO;
    self.relatedContainer.hidden = YES;

    // SHOW THE REVIEWS
    [self showOther];
} else if (selectedIndex == 2) {
    // Show related and hide details & reviews
    self.relatedContainer.hidden = NO;
    self.reviewsContainer.hidden = YES;
}
}

-(void)showOther {
// Animate the reviews
[UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:.9 initialSpringVelocity:1 options:UIViewAnimationOptionTransitionNone animations:^{
    // Hide details controls
    self.profileImageView.alpha = 0;
    self.seperatorImageView.alpha = 0;
    self.byLabel.alpha = 0;
    self.authorLabel.alpha = 0;

    // Move segmentControl
    [self.overviewSegmentControl setFrame:CGRectMake(self.overviewSegmentControl.frame.origin.x, self.previewImageView.frame.size.height + 8, self.overviewSegmentControl.frame.size.width, self.overviewSegmentControl.frame.size.height)];
}completion:^(BOOL finished) {
    // Completed
}];
}

-(void)showDetails{
// Animate the details
[UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:.9 initialSpringVelocity:1 options:UIViewAnimationOptionTransitionNone animations:^{
    // Move segmentControl
    [self.overviewSegmentControl setFrame:CGRectMake(self.overviewSegmentControl.frame.origin.x, self.previewImageView.frame.size.height + 85, self.overviewSegmentControl.frame.size.width, self.overviewSegmentControl.frame.size.height)];

    // Hide details controls
    self.profileImageView.alpha = 1;
    self.seperatorImageView.alpha = 1;
    self.byLabel.alpha = 1;
    self.authorLabel.alpha = 1;
}completion:^(BOOL finished) {
    // Completed
}];
}

The 2nd segment works perfect - where a few controls are fade out and the segment control slides up. However, 1st segment makes the segment snap back down and rapidly visualise the other controls instead of doing the same nice animation that happens with segment 1.

Any ideas why this happens and how I can fix it?

EDIT:

ViewController:

enter image description here

The controls below the big UIImageView are supposed to fade away while the segment control slides up right below the main UIImageView. The opposite when the -(void)showDetails method is called.

Thanks! Erik

Erik
  • 2,500
  • 6
  • 28
  • 49
  • You have a 3-part if/then/else statement for values 0, 1, and 2. In case 1, yo call your method showOther, which does an animation. In the segment 0 case, you call "showDetails". In the segment 3 case, you don't call ANY animation method. Your animation code manipulates the alpha of some views and moves your segmented control around. Without knowing what your different views, how they are used, and what they are supposed to do, it's pretty much impossible for your readers to be able to tell what is supposed to happen, much less what is going wrong. Post more details. – Duncan C Oct 24 '14 at 18:48
  • @DuncanC I see, I posted more details now, let me know if I should explain more:) – Erik Oct 24 '14 at 18:54
  • @DuncanC Any idea why it wouldn't work? See my new details. It annoys me beyond max. :) – Erik Oct 24 '14 at 19:55
  • @DuncanC after a lot of experimenting, it looks like the UISegmentControl returns to it's original state when clicked, even though no code is being called. Could I subclass UISegmentControl and go from there? – Erik Oct 24 '14 at 20:40
  • I don't understand what you are saying. – Duncan C Oct 24 '14 at 22:00

0 Answers0