拖动特定对象

时间:2010-12-02 11:51:07

标签: iphone sdk

我在iPhone sdk中拖动对象有问题,我的UIImageView插座是红色的,我不知道为什么拖动不起作用!但删除if行时我的代码工作正常,这里是代码:

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


    UITouch *touch = [[event allTouches]anyObject];

    if ([touch view] == red) {
    CGPoint location = [touch locationInView:self.view];
    red.center = location;  

    }
}


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

        [self touchesBegan:touches withEvent:event];
    }

2 个答案:

答案 0 :(得分:1)

当你覆盖UIView子类的touchesBegan时,只处理传递给那个UIView的触摸。

对于其子视图,您需要将UserInteractionEnabled设置为TRUE。然后为子视图创建一个子类,通过该子类可以将触摸处理委托给SuperView。

如果这听起来很复杂,请使用 - (UIView *)hitTest:(CGPoint)指向withEvent:(UIEvent *)事件

答案 1 :(得分:0)

嗨我用下面的代码做了。我必须以特定的水平和垂直方式拖动imageEnd,所以我将它们绑定在下面。

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

UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: touch.view];
location.x=510.0;
[imgEndPoint setUserInteractionEnabled:TRUE];

if ([touch view] == imgEndPoint) {
    if (location.y < 710.0 && location.y > 75.0) {//image move between this coordinate
        imgEndPoint.center = location;
    }
}
}

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

UITouch *touch = [[event allTouches] anyObject];

if ([touch view] == imgEndPoint) {

    CGPoint location = [touch locationInView: self.view];
    location.x=510.0;
    if (location.y < 710.0 && location.y > 75.0) {
    imgEndPoint.center = location;
        [self getPixcel];
    }
    [imgEndPoint setUserInteractionEnabled:TRUE];
}
}