Cannot change color for the second page by shake


#1

The color could be changed by shake when it was ‘UIView’ instance.

When it became UIScrollView with panning or paging, the color changed on the first page, but not on the second page. Why was that?


#2

@tomauhk I had the same issue. In the HypnosisViewController.m file, tell the HypnosisView instance to become first responder.


#3

@BryanLuby That’s right. It could be made by setting either HypnosisView instance as first responder. But I’m just wondering if ‘active screen’ is able to respond to the shake event.

Is there a BOOL check about the status of active screen, something like [view isActiveScreen] such that the first responder can be assigned to the active UIView instance?


#4

@tomauhk I looked in the book in chapter 7 on view controllers. Page 167 of the print version under “Appearing and disappearing views” has a good roundup of the different methods that can be overridden to handle changing views. In this case, it seems like viewDidAppear: is the method to override to give first responder status to the active view. The viewDidAppear: method will get called when its view has been added to the window. Here is what I added to TimeViewController.m:

[code]- (BOOL)canBecomeFirstResponder
{
return YES;
}

  • (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
    if (motion == UIEventSubtypeMotionShake) {
    NSLog(@“Device started shaking!”);
    [[self view] setBackgroundColor:[UIColor magentaColor]];
    }
    }
    [/code]
    Then added the viewDidAppear: override, telling the view to become first responder when its view appears:

- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear]; NSLog(@"TimeViewController did appear"); [self becomeFirstResponder]; }
Now shake events should register for each screen that becomes active.


#5

@BryanLuby You are so great and thank you for sharing. I’ve just started Chapter 7 and will dive into it soon. :smiley:


#6

Even in the downloadable “HypnoTime” Xcode file provided by Big Nerd Ranch, the shake color change for the circles on the Hypnosis screen do not work. I assume it’s something related to first responder but can’t get it working. Anyone figure this out?


#7

Fixed this. Once we move over to the HypnoTime app in chapter 7, you need to make sure to add the following to HypnosisViewController.m in the loadView method: