【问题标题】:Trouble with touch move触摸移动有问题
【发布时间】:2014-03-16 02:13:11
【问题描述】:

我想确保动作随着按钮按下图片移动(如在某些策略中)。但是当我移动它时图像会抖动。代码如下:

// PROPERTY
@property (nonatomic, retain) UIView *gameView;

@property GameState gameState; // for current state == Game
@property CGPoint touchPoint;
@property CGPoint screenOffset;

// INICIALIZE
_gameView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

UIGraphicsBeginImageContext(_gameView.frame.size);
[[UIImage imageNamed:iname] drawInRect:_gameView.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

_gameView.backgroundColor = [UIColor colorWithPatternImage:image];
_gameView.userInteractionEnabled = YES;

[self.view addSubview:_gameView];

// METHODS
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    switch (_gameState) {
        case Start:
            break;

        case Game:
            if ([touch view] == _gameView) {
                _touchPoint = [touch locationInView:touch.view];
                NSLog(@"startTouch");
            }
            break;

        default:
            break;
    }
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint currentTouchPoint = [touch locationInView:touch.view];
    switch (_gameState) {
        case Start:
            break;

        case Game:
            if ([touch view] == _gameView) {
                NSLog(@"%f%f", [touch locationInView:touch.view].x, [touch locationInView:touch.view].y);

                currentTouchPoint.x += _screenOffset.x - _touchPoint.x;
                currentTouchPoint.y += _screenOffset.y - _touchPoint.y;

                _gameView.center = currentTouchPoint;
            }
            break;

        default:
            break;
    }
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint endedTouchPoint = [touch locationInView:touch.view];
    switch (_gameState) {
        case Start:
            break;

        case Game:
            if ([touch view] == _gameView) {
                _screenOffset.x += endedTouchPoint.x - _touchPoint.x;
                _screenOffset.y += endedTouchPoint.y - _touchPoint.y;

                _gameView.center = _screenOffset;
                NSLog(@"endTouch");
            }
            break;

        default:
            break;
    }
}

即使稍微移动鼠标 - 图像也会跳跃。以下是日志中显示的内容:

startTouch
206.000000    319.000000
206.000000    338.000000
209.000000    342.000000
211.000000    367.000000
217.000000    393.000000
228.000000    435.000000
291.000000    580.000000
244.000000    470.000000
316.000000    620.000000
250.000000    470.000000
330.000000    620.000000
263.000000    468.000000
...
endTouch

同时,我将指针向一个方向移动。哪里错了?

我不想移动所有对象。 我找到理由。如果写:

_gameView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"field1.png"]];
_gameView.frame = [[UIScreen mainScreen] bounds]; 
//_gameView.userInteractionEnabled = YES; 

这是工作,但如果取消注释最后一个字符串问题返回。但它需要我的程序 =(

【问题讨论】:

    标签: ios shake touchmove


    【解决方案1】:

    您可以确保触摸的位置在 AddView 的父视图的坐标中给出:

    CGPoint location = [touch locationInView:AddView.superview];
    

    【讨论】:

    • 我将 UIView *gameView 替换为 UIImageView *gameView 并且它有效。但是为什么不使用 UIView 呢?
    • 以及如何检查条件([touch view] == gameView)?
    • 您正在移动包含所有视图的整个窗口,您应该只移动您想要的视图。
    • 看看这个链接可能对你有帮助stackoverflow.com/questions/4488823/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多