0

我创建了一个类 Box,它是 UIImageView 的子类。

在我的程序中,我想让 Box 成为主视图控制器中唯一的可拖动对象。

这是我的代码:

BoxViewController.m

-(void) viewDidLoad{
    [super viewDidLoad];
    target = [[Box alloc] initWithFrame:CGRectMake(10,10,100,100)];
    [self.view addSubView:target];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [[event allTouches] anyObject];
    if([touch view] == target){
          CGPoint location = [touch locationInView:self.view];
          target.center = location;
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    [self touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event];
}

我认为条件不起作用,但我尝试过 isKindOfClass。两者都没有工作。解决办法是什么?非常感谢。

4

1 回答 1

0

试试这个方法

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint location = [[touches anyObject] locationInView:self.view];
    target.transform = CGAffineTransformMakeTranslation(location.x, location.y);
}
于 2013-05-30T09:13:23.780 回答