可拖动的特定类,它是UIImageView的子类

时间:2013-05-28 17:50:05

标签: xcode

我创建了一个类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。两者都没用。解决办法是什么?非常感谢你。

1 个答案:

答案 0 :(得分:0)

尝试这种方法

(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint location = [[touches anyObject] locationInView:self.view];
    target.transform = CGAffineTransformMakeTranslation(location.x, location.y);
}