如何用摇动来改变uiview的大小

时间:2011-03-30 12:10:57

标签: iphone objective-c ipad uiview

我想用ipad摇动更改子视图大小,我有UIView包含子视图如何检测摇动并根据它做出响应

任何建议

4 个答案:

答案 0 :(得分:0)

How to detect iphone shake gesture one time !

并且在检测到震动后,调用将改变视图大小的动作。 例如,[myview setFrame:CGRectMake(0,0,150,150)];

答案 1 :(得分:0)

Apple有一个关于如何处理这些问题的小部分:Motion Events。基本上,您可以从继承自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 {

}

答案 2 :(得分:0)

motion events作为UIEvents传递,您可以利用它们。一旦动作结束并且您已捕获摇动事件,您可以根据需要调整视图大小。

答案 3 :(得分:0)

如果您定位iOS 3.0及更高版本,则可以使用摇动设备生成UIEvent子类型UIEventSubtypeMotionShake的事实。 Event Handling Guide: Motion Events - Shaking-Motion Events

您的UIView应该覆盖以下方法:

- (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];
   }
}