2010-12-02 73 views
1

我有一个在iPhone SDK中拖动对象的问题,我的UIImageView插座是红色的,我不知道为什么拖动不起作用!但是,当删除,如果行我的代码工作正常,这里是代码:拖动特定的对象

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

您必须在CGTRE“touchesMoved”上写下您的对象的位置 – Momi 2010-12-02 12:23:27

回答

1

当你重写一个UIView子类的touchesBegan,只提供给UIView的触摸被处理那里。

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

如果这听起来很复杂,只需使用 - (UIView的*)则hitTest:(CGPoint)点withEvent:方法(*的UIEvent)事件

+0

谢谢,momeks权利! ,我想在屏幕上拖动这个对象 – 2010-12-02 12:40:59

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