1

I have a UIView property named viewToFade that contains a UISegmentedControl. When I animate the UIView (so it appears on tapping and gradually fades), the UISegmentedControl doesn't respond to touches. Here's the code I'm using:

-(void)fadeView
{
self.viewToFade.alpha=1;
self.viewToFade.hidden=NO;

//When I comment the following lines out, everything is fine--the `UISegmentedControl` responds as it should.  
//But when I include them, the `UISegmentedControl` doesn't register changes/taps.

[UIView animateWithDuration:0.3
                      delay: 3.0
                    options:UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     self.viewToFade.alpha = 0.0;
                 }
                 completion:nil];
}

What am I doing wrong?

(In case it's relevant, I'll say that the UIView and the UISegmentedControl are both created in the xib file.)

EDIT: I figured out a way to deal with the problem, which was to rewrite the code as follows:

-(void)fadeView
{
self.viewToFade.alpha=1;
self.viewToFade.hidden=NO;
[self performSelector:@selector(disneyfy) withObject:nil afterDelay:(3)];
} 

-(void)disneyfy
{
    [UIView animateWithDuration:0.3
                 animations:^{
                     self.viewToFade.alpha = 0.0;
                 }];
}
Joel Derfner
  • 2,207
  • 6
  • 33
  • 45
  • This is unexpected to me. Not sure if it is relevant, but what OS version are you running it on? – lindon fox Sep 07 '12 at 03:57
  • Yes I met the same issue, parent-view's alpha **will change** if child-view's alpha changed, but child-view's alpha **won't change** if parent-view's alpha changed. – Mil0R3 Sep 07 '12 at 04:03
  • This question should help: http://stackoverflow.com/q/8346100/937822 – lnafziger Sep 07 '12 at 04:27
  • I guess I am misunderstanding, if viewToFade contains the segmented control, it will disappear when it's parent alpha = 0.0. A view won't respond to touches when it's alpha=0.0 (or it's ancestors). But it's also not visible to touch. Am I mixed up on this? – danh Sep 07 '12 at 04:43
  • 1
    @danh: The system considers the alpha to be 0 before the animation even starts. It does NOT gradually change the alpha. That is, after half the animation is done, the alpha is not 0.5. It will go from 1 to 0, no in-between values. – Scott Berrevoets Sep 07 '12 at 06:05

1 Answers1

1

I played around with this in a sample project and could duplicate your results - nothing I did even animating (in my case) a button itself rendered it immune to touches. However in my limited test I was able to see that it did respond to highlighting.

I just tested the following (using a UIBuutton, suppose the same thing will work for segmented control):

  • add a transparent view over your button, and add a gesture recognizer to it, just before the animation

  • have the actionMethod of the tap recognizer change the highlighting of the button on, then dispatch a block to turn it off in 100ms or so (you could get fancier if you actually intercept touches).

  • in addition to changing the highlighting, send something like this to the control (again using a button) :

    [tapButton sendActionsForControlEvents:UIControlEventTouchUpInside];
    

It appears the control is responsive to such things during the animation.

David H
  • 40,852
  • 12
  • 92
  • 138
  • Oooh, this looks great! Unfortunately, I'm too much a newbie to know how to implement it--I don't understand blocks (I got the block code I used from the Apple documentation). I did find a solution to my problem (original question edited), but I suspect it's far less elegant than this. – Joel Derfner Sep 07 '12 at 14:38
  • If its just blocks you are cannot deal with, you can use one of the 'performSelector' messages that has 'after' parameter. Otherwise, offer a bound of 50 pts to the person who codes this for you. You need to specify the number of segments, if they are momentary or selectable. – David H Sep 07 '12 at 15:12