Reputation: 543
Here's a more info on my sitation...I have a tableview that when you touch a cell within the tableview, it adds a custom view called DraggableView to the window on the exact same position as the tableview row you selected, and the same size.
EDIT: Here's how I added the DraggableView subview to the view. This code is on the custom tableviewcell class:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UIView *topmostView = [[[super superview] superview] superview];
UITableView *parentTableView = (UITableView *)[self superview];
CGPoint draggablePoint = parentTableView.frame.origin;
CGSize draggableSize = parentTableView.frame.size;
draggablePoint.y += indexPath.row * 44;
dragView.frame = CGRectMake(draggablePoint.x, draggablePoint.y, draggableSize.width, 44);
[topmostView addSubview:dragView];
}
This draggable view is made so it is draggable. On it's designated initializer, it adds a UIPanGestureRecognizer to the view so you can pan it around. I do this like so:
- (void) activatePan {
UIPanGestureRecognizer *pan =
[[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panMove:)] autorelease];
[self addGestureRecognizer:pan];
}
- (void) panMove:(UIPanGestureRecognizer*)recognizer {
CGPoint touchLocation;
if (recognizer.state == UIGestureRecognizerStateBegan) {
currentFrame = self.frame;
touchLocation = [recognizer locationInView:self];
}
CGPoint translatedPoint = [(UIPanGestureRecognizer*)recognizer translationInView:self];
//The object is being moved across the screen
self.frame = CGRectMake(currentFrame.origin.x + translatedPoint.x, currentFrame.origin.y + translatedPoint.y, currentFrame.size.width, currentFrame.size.height);
}
What I need is for this newly created Draggable View to have the user's "focus" when his finger is still on top of the row. This is for the draggable view to be draggable once it's created.
What happens as of now, is that the draggable view is created, but I have to release my finger, and tap the draggable view to pan it around. I wish to skip the step of releasing the finger and touch the view, so I can drag it once it's been created. Any ideas?
Upvotes: 0
Views: 864
Reputation: 1283
From the Apple documentation:
- (BOOL)becomeFirstResponder
"You may call this method to make a responder object such as a view the first responder. However, you should only call it on that view if it is part of a view hierarchy. If the view’s window property holds a UIWindow object, it has been installed in a view hierarchy; if it returns nil, the view is detached from any hierarchy."
Upvotes: 1