ios - 获取 subview 枚举的框架

标签 ios objective-c uiview frame subview

因此,我有包含缩略图的重复 View ,按下缩略图后,缩略图 ID 将作为标签发送。

有了这些信息,我想获取该 View 的 subview 的框架;

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

我想要获取框架的 subview

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 框架(记住有多个)。

最佳答案

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

- (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

假设您想要与按下的按钮位于同一拇指 View 中的 ImageView 。

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

@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
}

关于ios - 获取 subview 枚举的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33359025/

相关文章:

ios - 无法使用协变类型 'refreshControl' 覆盖类型 'UIRefreshControl?' 的可变属性 'UIRefreshControl'

ios - 如何使用 XCTestCase 测试异步代码

iOS 8. 在没有任何通知的情况下更改应用角标(Badge)编号

ios - 核心数据模型版本控制,没有 xcdatamodeld 文件

iphone - 如何在 iphone 中创建具有 Marquee Effect 的动态多个 uiview

iOS/Xcode——drawRect : Not Being Called in UIView Subclass

ios - IOS 13 上的后台任务(BGTaskScheduler)

ios - 表格 View 选择和平移手势

objective-c - 两个应用程序之间的通信

ios - 如何以编程方式触发 UIView 的点击手势识别器