1

I want to change subview size with ipad shake , I have a UIView contains a subview how to detect the shake and respond according to it

any suggestion to do that

4 Answers 4

0

How to detect iphone shake gesture one time !

and after shake is detected, call action that will change size of the view. Eg,
[myview setFrame:CGRectMake(0,0,150,150)];

0

Apple have a nice little section on how to handle those: Motion Events. Basically, you can override three different methods from any class that inherits from UIResonder:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {

}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {    
    [UIView beginAnimations:nil context:nil];    
    [UIView setAnimationDuration:0.5];

    self.view.transform = CGAffineTransformIdentity;

    for (UIView *subview in self.view.subviews) {    
        subview.transform = CGAffineTransformIdentity;
    }

    [UIView commitAnimations];

    for (TransformGesture *gesture in [window allTransformGestures]) {    
        [gesture resetTransform];    
    }    
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {

}
0

There are motion events that are passed as UIEvents that you can take advantage of. Once the motion has ended and you have captured the shake event you can resize your view as necessary.

0

If you target iOS 3.0 and later, you could use the fact that shaking the device generates a UIEvent with subtype UIEventSubtypeMotionShake. Event Handling Guide: Motion Events - Shaking-Motion Events

Your UIView should override the following methods:

- (BOOL)canBecomeFirstResponder
{
  return YES;
}

- (void)viewDidAppear:(BOOL)animated
{
  [self becomeFirstResponder];
}

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
   // Clean what you need to
}

- (void) motionEnded:(UIEventSubType)motion withEvent:(UIEvent *)event
{
   if (UIEventSubtypeMotionShake == motion)
   {
      // Resize your view
   }
   else
   {
     [super motionEnded:motion withEvent:event];
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.