Dec*_*nna 19 objective-c touch uitouch ios
我希望将UIImage拖到几个UIButtons中的一个上,并根据我拖动它的按钮重新定位它.
我遇到的问题是UITouch只记录触摸开始的视图,我想访问我的触摸结束的视图.我怎样才能做到这一点?
码:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
dealerBtnOrigin = [touch locationInView:self.view];
//NSLog(@"%u %u",touch.view.tag, ((UIView *)[dealerBtns objectAtIndex:[table getButtonSeat]]).tag);
//CHECK TO SEE WHICH BUTTON WAS TOUCHED
if (touch.view == ((UIView *)[dealerBtns objectAtIndex:[table getButtonSeat]]))
{
((UIView *)[dealerBtns objectAtIndex:[table getButtonSeat]]).center = location;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
if (touch.view == ((UIView *)[dealerBtns objectAtIndex:[table getButtonSeat]]))
{
((UIView *)[dealerBtns objectAtIndex:[table getButtonSeat]]).center = location;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
UIView *endView = [self.view hitTest:location withEvent:nil];
if (touch.view == ((UIView *)[dealerBtns objectAtIndex:[table getButtonSeat]]))
{
NSLog(@"%u %u",endView.tag, touch.view.tag);
if([buttons containsObject:(UIButton *)endView])
{
[[dealerBtns objectAtIndex:[table getButtonSeat]] setHidden:YES];
[table passButton:touch.view.tag];
[[dealerBtns objectAtIndex: touch.view.tag] setHidden:NO];
}
((UIView *)[dealerBtns objectAtIndex:[table getButtonSeat]]).center = dealerBtnOrigin;
}
}
Run Code Online (Sandbox Code Playgroud)
mat*_*att 23
使用命中测试.根据最终的超级视图,你知道这个点作为CGPoint的位置self.view.然后你可以问self.view它指向哪个子视图:
CGPoint location = [touch locationInView:self.view];
UIView* v = [self.view hitTest:location withEvent:nil];
Run Code Online (Sandbox Code Playgroud)
UIView v是您正在寻找的视图.
Thi*_*ama 16
检测最终触摸的视图非常容易,请尝试此代码
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint location = [[touches anyObject] locationInView:self.view];
CGRect fingerRect = CGRectMake(location.x-5, location.y-5, 10, 10);
for(UIView *view in self.view.subviews){
CGRect subviewFrame = view.frame;
if(CGRectIntersectsRect(fingerRect, subviewFrame)){
//we found the finally touched view
NSLog(@"Yeah !, i found it %@",view);
}
}
}
Run Code Online (Sandbox Code Playgroud)