ScrollView 未使用 imageview 更新
Posted
技术标签:
【中文标题】ScrollView 未使用 imageview 更新【英文标题】:ScrollView not updated with imageview 【发布时间】:2013-12-18 08:53:29 【问题描述】:我的主包中有 20 张图像,我想在我的滚动视图中水平显示它。
我做得很成功,但现在的问题是,在所有图像视图都设置在滚动视图中之后,图像在滚动视图中可见,这需要时间。
为了解决我创建了一个 nsoperation。
-(BOOL)sync:(NSError**)error
float imageLeftPosition = 0;
for (int imageCount = 1; imageCount<=syncObject.syncNumberOfImages; imageCount++)
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(imageLeftPosition, 0, syncObject.syncImageWidth, syncObject.syncImageHeight)];
[imgView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d_%d.jpg",syncObject.syncChapterNumber,imageCount]]];
imgView.tag = imageCount;
// NSDictionary *dictionary = [NSDictionary new];
//
// [dictionary setValue:imgView forKey:CHAPTER_IMAGE];
imageLeftPosition = imageLeftPosition+syncObject.syncImageWidth;
//[dictionary setValue:[NSString stringWithFormat:@"%f",imageLeftPosition] forKey:CHAPTER_SCROLL_LEFT_POSITION];
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:imgView,[NSString stringWithFormat:@"%f",imageLeftPosition], nil] forKeys:[NSArray arrayWithObjects:CHAPTER_IMAGE,CHAPTER_SCROLL_LEFT_POSITION, nil]];
[self sendNotification:dictionary];
imgView = nil;
dictionary = nil;
return YES;
我在这里做的是添加创建一个图像视图并使用通知将其发布到主视图
-(void)addImageView:(NSNotification*)notification
NSInteger thread = [NSThread isMainThread]?CVC_MainThread:CVC_BackgroundThread;
switch (thread)
case CVC_MainThread:
NSDictionary *dictionary = notification.userInfo;
if(dictionary != nil)
NSLog(@"image view : %@",(UIImageView*)[dictionary valueForKey:CHAPTER_IMAGE]);
NSLog(@"leftposition: %f",[[dictionary valueForKey:CHAPTER_SCROLL_LEFT_POSITION] floatValue]);
[_scrChapters addSubview:((UIImageView*)[dictionary valueForKey:CHAPTER_IMAGE])];
_scrChapters.contentSize = CGSizeMake([[dictionary valueForKey:CHAPTER_SCROLL_LEFT_POSITION] floatValue], 0);
dictionary = nil;
break;
case CVC_BackgroundThread:
dispatch_async(dispatch_get_main_queue(), ^(void)
NSDictionary *dictionary = notification.userInfo;
if(dictionary != nil)
NSLog(@"image view : %@",(UIImageView*)[dictionary valueForKey:CHAPTER_IMAGE]);
NSLog(@"leftposition: %f",[[dictionary valueForKey:CHAPTER_SCROLL_LEFT_POSITION] floatValue]);
[_scrChapters addSubview:((UIImageView*)[dictionary valueForKey:CHAPTER_IMAGE])];
_scrChapters.contentSize = CGSizeMake([[dictionary valueForKey:CHAPTER_SCROLL_LEFT_POSITION] floatValue], 0);
dictionary = nil;
);
break;
default:
break;
这是通知处理程序方法,我在其中为主线程上的每个循环设置滚动视图中的内容大小和图像视图,在所有图像加载后仍然可见滚动视图。
我做错了什么?
【问题讨论】:
在此 [[dictionary valueForKey:CHAPTER_SCROLL_LEFT_POSITION] floatValue] 中使用静态值作为图像计数*图像大小进行交叉检查,内容大小是否正确。 【参考方案1】:尝试使用pagedflowview。这只会将可见项目加载到 Scrollview 中,并且易于自定义。您也可以搜索 iCarousel。
https://www.cocoacontrols.com/controls/pagedflowview
【讨论】:
是的,这是一种选择,但我想知道当前逻辑的问题所在。【参考方案2】:试试这个
-(void)showimagesasSlide:(int) totalimagesCount
int xAxisofimageView=0;
for (int initialimagesCount=0;initialimagesCount<totalimagesCount; initialimagesCount++)
UIImageView *detailedImageview=[[UIImageView alloc] init];
detailedImageview.userInteractionEnabled=YES;
UIImage *dynamicImage=[ScaleImageSize squareImageWithImage:[images objectAtIndex:initialimagesCount] scaledToSize:CGSizeMake(300, 500) ];
if (IS_IPHONE_5)
detailedImageview.frame=CGRectMake(xAxisofimageView, 0, 320, 568);
else
detailedImageview.frame=CGRectMake(xAxisofimageView, 0, 320, 460);
// detailedImageview.contentMode=UIViewContentModeCenter;
detailedImageview.backgroundColor=[UIColor clearColor];
detailedImageview.image=dynamicImage;
detailedImageview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin ;
detailedImageview.tag=initialimagesCount+12;
[mainScrollview addSubview:detailedImageview];
xAxisofimageView=xAxisofimageView+320;
mainScrollview.contentSize=CGSizeMake(320*images.count, 0);
【讨论】:
以上是关于ScrollView 未使用 imageview 更新的主要内容,如果未能解决你的问题,请参考以下文章
在 iOS 6 上使用自动布局在 ScrollView 中嵌入 ImageView
使用 scrollView 和 imageView 的 bounds.height 属性计算 zoomScaleForHeight
Autolayout 使 ScrollView 中的 ImageView 无法滚动
如何从 ScrollView 中的 ImageView 中选择项目?