UIScrollview图像无限滚动(图像加载)问题ios



我正在尝试在UIScrollView上显示图像,所有这些图像都是水平显示的,但是,我在滚动加载图像时遇到问题。

我尝试使用此代码并将UILabel修改为UIImageview,并在scrollViewDidEndDecelerating中滚动时调用异步图像下载 - 图像滚动循环,一个只有3页。它适用于"文本标签"示例,但是当我异步加载图像(也缓存)时,会出现闪烁在滚动上。

假设,如果我从第 0 页滚动到第 1 页,然后第 0 页闪烁

了一下,然后可以再次看到第 1 页。

知道如何摆脱这个问题吗?

尝试使用当前主队列添加图像:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.scrollView addSubview:imageView];
});

我的 .m 类你需要一个视图,并在该视图上添加一个 imageViewSlider(uiimageview)。 将其与活动指示器和分离的图像获取相结合,看看它是否有效。这实现了没有滚动视图的幻灯片

#import "ImageSliderView.h"
#import <QuartzCore/QuartzCore.h>
#define kAnimationKey @"animationKey"
@interface ImageSliderView ()
@end
@implementation ImageSliderView
@synthesize imageData;
@synthesize imageViewSlider;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Set GestureRecoginzer..
    //******************************************************Swipe recognisers**************************
    UISwipeGestureRecognizer *rightRecognizer=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeHandle:)];
    rightRecognizer.direction=UISwipeGestureRecognizerDirectionRight;
    [rightRecognizer setNumberOfTouchesRequired:1];
    [self.view addGestureRecognizer:rightRecognizer];
    [rightRecognizer release];
    UISwipeGestureRecognizer *leftRecognizer=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeHandle:)];
    leftRecognizer.direction=UISwipeGestureRecognizerDirectionLeft;
    [leftRecognizer setNumberOfTouchesRequired:1];
    [self.view addGestureRecognizer:leftRecognizer];
    [leftRecognizer release];
    //******************************************************
    currentPage=0;
        // Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
    [self setImageData:nil];
    [self setImageViewSlider:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
-(void)dealloc
{
    [imageData release];
    [imageViewSlider release];
    [super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark ---swipe
-(void)swipeHandle:(UISwipeGestureRecognizer *)guestureRecognizer
{
    if(guestureRecognizer.direction==UISwipeGestureRecognizerDirectionLeft)
    {
         [self nextButtonAndSlideForwad];
    }
    if(guestureRecognizer.direction==UISwipeGestureRecognizerDirectionRight)
    {
                [self prevButtonAndSlideBack];
    }
}

-(void)nextButtonAndSlideForwad
{

    self.imageViewSlider.backgroundColor=[UIColor redColor];
            [self slideForward];

}
-(void)prevButtonAndSlideBack
{
    self.imageViewSlider.backgroundColor=[UIColor greenColor];
    [self slideBackward];
}
-(void)slideForward
{
    CATransition *animation = [CATransition animation];
    [animation setDelegate:self];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromRight];
    [animation setDuration:0.4f];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; 
    [[self.view layer] addAnimation:animation forKey:kAnimationKey];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.55];
    [UIView commitAnimations];

}
-(void)slideBackward
{
    CATransition *animation = [CATransition animation];
    [animation setDelegate:self];
    [animation setType:kCATransitionPush];
    [animation setSubtype:kCATransitionFromLeft];
    [animation setDuration:0.4f];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0]; 
    [[self.view layer] addAnimation:animation forKey:kAnimationKey];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.55];
    [UIView commitAnimations];
}
@end

最新更新