【问题标题】:Getting frame of subview enumeration获取子视图枚举的框架
【发布时间】:2015-10-27 02:33:12
【问题描述】:

所以,我有包含缩略图的重复视图,一旦按下缩略图,缩略图 ID 就会作为标签发送。

有了这些信息,我想得到那个视图的子视图的框架;

- (IBAction)thumbPressed:(id)sender {
    NSLog(@"Thumb %i pressed", (int)[sender tag]);

    for (int i = 0; i < _thumbCounter; i++) {
        if (i == [sender tag]) {
            NSLog(@"thumb view %i", i);

            //Up To HERE

            break;
        }
    }

//    [self moveToVideoControllerWithInfoID:[NSString stringWithFormat:@"%li", (long)[sender tag]]];
}

对于要绘制的缩略图,它们是按照 JSON 检索的随机顺序绘制的。

按钮

UIButton *thumbButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[thumbButton addTarget:self
           action:@selector(thumbPressed:)
 forControlEvents:UIControlEventTouchUpInside];
thumbButton.frame = CGRectMake(0, 0, _thumbView.frame.size.width, _thumbView.frame.size.height);
thumbButton.tag = _thumbCounter;
[_thumbView addSubview:thumbButton];

我要获取框架的子视图

NSString *string = [NSString stringWithFormat:@"***.jpg",Link];

NSURL * imageURL = [NSURL URLWithString:string];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(thumbGap, thumbGap, thumbHeight - 5, thumbHeight - 5)];
imageView.image = image;

[_thumbView addSubview:imageView];

缩略图壳的绘制位置

_thumbView = [[ThumbView alloc] initWithFrame:CGRectMake(0, margin, _scrollView.frame.size.width, thumbHeight)];
_thumbView.backgroundColor = [UIColor whiteColor];
_thumbView.thumbId = _thumbCounter;
[_scrollView addSubview:_thumbView];

_thumbview 是添加了 thumbId 的 UIVIEW 类

一旦按下该按钮,我如何才能在 _thumbview 中找到 imageView 框架(记住有多个)。

【问题讨论】:

  • 我只是想确定你的逻辑。如果你能把它画成
      /ul> 格式的树形图会更容易,你能帮忙吗?

标签: ios objective-c uiview frame subview


【解决方案1】:

首先,让我们让您的生活更轻松:

- (IBAction)thumbPressed:(UIButton*)sender {
    NSLog(@"Thumb %i pressed", (int)[sender tag]);
    ThumbView *thumbView = (ThumbView*)[sender superView];
    for(UIView* subview in thumbView.subViews) {
        if([subView iKindOfClass:[UIImageView class]]) {
            //you got it here
        }
    }
}

你可以通过让 ThumbView 也有一个 imageView 属性来简化整个事情。

我试图确定这就是你正在做的事情:

_scrollView
->_thumbView[0]
--->thumbButton
--->imageView
->_thumbView[1]
--->thumbButton
--->imageView
...
->_thumbView[N]
--->thumbButton
--->imageView

假设您想要与按下的按钮在同一个 thumbView 中的图像视图。

最佳可能解决方案(恕我直言):

@Interface ThumbView()
    @property (nonatomic,weak) UIImageView *imageView;
@end

与: (确保先添加,然后设置 .imageView)

- (IBAction)thumbPressed:(UIButton*)sender {
    NSLog(@"Thumb %i pressed", (int)[sender tag]);
    UIImageView *imageView = ((ThumbView*)[sender superView]).imageView
}

【讨论】:

  • 感谢您的回答。我用更多的手指努力得出了同样的结论,我会接受你的回答并发布我自己的。
  • 更好的是,你是一个非常聪明的人。感谢您付出的额外努力!
【解决方案2】:
for (UIView *i in _scrollView.subviews) {
    if ([i isKindOfClass:[ThumbView class]]) {
        ThumbView *tempThumb = (ThumbView *)i;
        if (tempThumb.thumbId == (int)[sender tag]) {
            for (UIView *y in tempThumb.subviews) {
                if ([y isKindOfClass:[UIImageView class]]) {
                    UIImageView *tempIMG = (UIImageView *)y;

                    [self playVideo:[loadedInput objectForKey:@"link"] frame:tempIMG.frame andView:tempThumb];
                }
            }
        }
    }
}

【讨论】:

  • 虽然这是一个很好的努力,但我相信它可以通过不那么极端的嵌套深度来完成。你最好的朋友是有问题的视图已经通过了,UIButton*
  • 我不是要批评,只是从不断审查和维护代码的人那里得到它,如果您使用较少的深度嵌套,您将来会感谢自己的,相信我:)
  • @MitchellCurrie 由于我缺乏视图经验(用于精灵套件)我缺乏更简单的术语。感谢您帮助我缩短代码!
  • 乐于助人。请检查修改后的解决方案的底部,了解我认为最优雅的内容。
猜你喜欢
  • 1970-01-01
  • 2020-03-29
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 2011-02-16
  • 1970-01-01
相关资源
最近更新 更多