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

时间:2013-01-29 09:40:17

标签: iphone ios uiscrollview uiimageview

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

我尝试使用此代码并将UILabel修改为UIImageview,并在scrollViewDidEndDecelerating - Image Scroll Circular中滚动调用异步图像下载,仅限3页。它适用于“文本标签”示例,但当我异步加载图像(缓存)时,则会出现闪烁 滚动。

假设,如果我从第0页滚动到第1页,那么第0页会闪烁一点,然后可以再次看到第1页。

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我的.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
相关问题