1
votes

I'm using :

[button addTarget:self action:@selector(moved:) forControlEvents:UIControlEventTouchDragInside];

Function code move:

- (IBAction)moved:(id)sender {
UIButton *t = sender;

UITouch *touch = [touch self];

CGPoint touchPoint = [touch locationInView: self];

t.center = touchPoint;
}

So, when I start to drag the button, it suddenly appears in left up corner (Coord: 0;0). I thought this was a problem because CGPoint touchPoint = [touch locationInView: self]; works in wrong way. I've got 3 classes: viewController, which makes implement of:

view = [[MainView alloc] initWithFrame:[self.view frame] ];
[self.view addSubview:view];

where view is object of class mainview(subclass uiview). In mainview i make object of class Button(subclass uiview):

[self addSubview:but];

Then in class Button i make UIButton, associating with event UIControlEventTouchDragInside(see beginning of post).

So how should i correct my code, that button move to touch point correctly???

P.S. Sorry for my awful English=)

1

1 Answers

2
votes

If you really want to stick with UIControlEventTouchDragInside then use the snippet below. Personally I would use UIPanGestureRecognizer instead.

- (IBAction)moved:(UIButton *)sender {
    UITouch *touch = [touch self];
    CGPoint touchPoint = [touch locationInView: sender.superview];
    sender.center = touchPoint;
}