我希望将UIImage拖到几个UIButton中的一个上,并根据我拖动它的按钮重新定位它。
我遇到的问题是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;
}
}
答案 0 :(得分:23)
使用命中测试。根据最终超级视图self.view
,您知道此点作为CGPoint的位置。然后,您可以询问self.view
指向哪个子视图:
CGPoint location = [touch locationInView:self.view];
UIView* v = [self.view hitTest:location withEvent:nil];
UIView v
是您正在寻找的视图。
答案 1 :(得分: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);
}
}
}
答案 2 :(得分:0)
通常,您需要确定触摸结束的点,并确定它是否位于您感兴趣的视图中。我使用类似于以下的代码执行此操作:
CGPoint newCenter = dragView.center;
for (UIView *v in dropTargets)
{
CGRect convertedTargetFrame = [v.superview convertRect:v.frame toView:nil];
if (CGRectContainsPoint(convertedTargetFrame, newCenter))
{
activeTargetIndex = idx;
}
}
(这段代码是动态编辑的,可以删除很多无关紧要的内容,但是我在dropTargets中保留了一个潜在的drop目标列表,这只是查看该列表以查看哪些潜在的UIViews可能包含点)。
重要的是,如果您知道可能将项目拖动到的视图或视图,则可以使用CGRectContainsPoint确定该视图的框架是否包含该点。重要的是要记住它们可能位于不同的坐标系中,在比较之前可能需要从一个坐标系转换为另一个坐标系。
答案 3 :(得分:0)
假设容器中有@property
个目标视图,并且目标视图已添加到容器中:
@property (nonatomic, strong) UIView* targetView;
// ...
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet* touchesForTargetView = [event touchesForView:self.targetView];
if (touchesForTargetView.allObjects.count != 0) {
// touch was at target view
}
else {
// touch was somewhere else
}
}
答案 4 :(得分:-1)
覆盖func touchesEnded(_ touches:Set,with event:UIEvent?){
''
//继续.. }
if let touch = touches.first{
let location = touch.location(in: self.view)
let v = touch.view