获取子视图枚举的框架
Getting frame of subview enumeration
因此,我有包含缩略图的重复视图,一旦按下缩略图,缩略图 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];
缩略图 shell 的绘制位置
_thumbView = [[ThumbView alloc] initWithFrame:CGRectMake(0, margin, _scrollView.frame.size.width, thumbHeight)];
_thumbView.backgroundColor = [UIColor whiteColor];
_thumbView.thumbId = _thumbCounter;
[_scrollView addSubview:_thumbView];
_thumbview 是 UIVIEW 的 class 添加了 thumbId
我如何在按下该按钮后找到 _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
假设您想要与按下的按钮位于同一 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
}
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];
}
}
}
}
}
因此,我有包含缩略图的重复视图,一旦按下缩略图,缩略图 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];
缩略图 shell 的绘制位置
_thumbView = [[ThumbView alloc] initWithFrame:CGRectMake(0, margin, _scrollView.frame.size.width, thumbHeight)];
_thumbView.backgroundColor = [UIColor whiteColor];
_thumbView.thumbId = _thumbCounter;
[_scrollView addSubview:_thumbView];
_thumbview 是 UIVIEW 的 class 添加了 thumbId
我如何在按下该按钮后找到 _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
假设您想要与按下的按钮位于同一 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
}
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];
}
}
}
}
}