【问题标题】:TouchesBegan with bringSubviewToFrontTouchesBegin with bringSubviewToFront
【发布时间】:2017-11-25 20:57:00
【问题描述】:

我正在为 Gestrue Recognizer 工作,它有许多 UIView 用于在视图中进行旋转、捏合和平移操作。 我的问题是当我触摸时如何使我的 UIView 中的一个bringSubviewToFront,例如,like my photo,我希望在触摸时将绿色的一个带到前面。 (所以蓝色变成绿色的后面)

每个 UIView 都是从我的 NoteView 类初始化的

NoteView * noteView = [[NoteView alloc]initWithFrame:CGRectMake(50, 50, 150, 150)];
[self setGesture:noteView];
[self.view addSubview:noteView];

TouchsBegan 我就是这样工作的

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.note = [NoteView new];

UITouch * touch = [touches anyObject];
if (touch.view == self.note) {
    [self.view bringSubviewToFront:self.note];
}}

【问题讨论】:

  • if (CGRectIntersectsRect(someView.frame, otherView.frame){[self.view bringSubViewToFront:oneOfTheView];}?
  • 在您的 touchesBegan:withEvent: 方法中,您要做的第一件事是创建一个新的 NoteView 并将其分配给一个 note 属性。 if (touch.view == self.note) 永远不会为真(触摸不能包含新的笔记视图,因为您刚刚创建了它),因此 if 语句范围内的代码将永远不会被执行。
  • 你指出我的错误!你能告诉我如何获得我接触到的当前视图吗?谢谢!

标签: ios objective-c uigesturerecognizer touch-event bringtofront


【解决方案1】:

感谢您的帮助!我解决了我的问题,也许有人也会有同样的问题,所以我更新了我的答案,我使用 user3589771 的link 来解决这个问题。

如下代码

-(void) touchesBegan:(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)){
        NSLog(@"Yeah !, i found it %@",view);
        [self.view bringSubviewToFront:view];
    }
}}

但其实我并没有完全理解

for(UIView *view in self.view.subviews){
    CGRect subviewFrame = view.frame;

    if(CGRectIntersectsRect(fingerRect, subviewFrame)){

    }}

顺便说一句,如果有人可以对这个新问题发表评论,那就太好了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多