【问题标题】:touchesBegin & touchesMove Xcode Obj C QuestiontouchesBegin & touchesMove Xcode Obj C 问题
【发布时间】:2010-06-16 01:37:17
【问题描述】:

因此,当您按下并拖动时,我的应用程序运行良好。我还在 Interface Builder 中将 UIButtons 设置为 Touch Down。

当您拖动时,您需要从 UIButton 的外部拖动。您不能单击 UIButton 并拖动到另一个。

触摸移动:

代码:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint location = [touch locationInView:touch.view];

if(CGRectContainsPoint(oneButton.frame, location))
{
    if (!oneButton.isHighlighted){
        [self oneFunction];
        [oneButton setHighlighted:YES];
    }
}else {
    [oneButton setHighlighted:NO];
}
//
if(CGRectContainsPoint(twoButton.frame, location)) 
{
    if (!twoButton.isHighlighted){
        [self twoFunction];
        [twoButton setHighlighted:YES];
    }
}else {
    [twoButton setHighlighted:NO];
}

}

开始接触:

代码:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

   UITouch *touch = [[event touchesForView:self.view] anyObject];

CGPoint location = [touch locationInView:touch.view];

if(CGRectContainsPoint(oneButton.frame, location))
{
    [self oneFunction];
    [oneButton setHighlighted:YES];
}
if(CGRectContainsPoint(twoButton.frame, location))
{
    [self twoFunction];
    [twoButton setHighlighted:YES];
}

}

我希望能够单击任何按钮触发该功能,并且还能够从一个按钮拖动到另一个按钮并触发该功能。

所以基本上只需点击一个按钮并将手指滑过并激活另一个按钮,而无需从按钮外部按下和滑动。

我想我已经很接近了,需要一些帮助。希望这足够清楚。

谢谢。

【问题讨论】:

  • 最好的地方是objective-c而不是c。
  • 该代码看起来不错。它没有做什么你想要的?
  • @Jeff 不起作用的是,当我点击其中一个 UIButton 时,我可以将手指滑到另一个 UIButton 以触发该功能。我必须从任何 UIButtons 外滑动手指。
  • 这些方法在哪里?视图控制器?
  • 是的,它在 UIViewController 内部 - MainViewController : UIViewController 是通过我的 RootViewController 拉出的,它将该视图分配为第一个。 MainWindow.xib -> MainView.xib 所以是的,它在视图控制器中。

标签: objective-c cocoa-touch uibutton ios


【解决方案1】:

尝试了一些不同的东西。使用它效果更好:

if(CGRectContainsPoint(myObject.frame, location) && lastButton != myObject) {

在 touchesBegan 和 touchesMoved 中使用它。

【讨论】:

    猜你喜欢
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    相关资源
    最近更新 更多