获取子视图枚举的框架

时间:2015-10-27 02:33:13

标签: ios objective-c uiview frame subview

因此,我有重复包含缩略图的视图,一旦按下缩略图,缩略图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框架(请记住有多个)。

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

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];
                }
            }
        }
    }
}