0

我写下面的代码来刷 2 图像。它工作正常,但现在我的任务是水平滑动超过 2 个图像。如果我们向左滑动,在最后一张图片上也会出现第一张图片

我怎样才能做到这一点?

-(void)viewDidLoad    
{
  [super viewDidLoad];

  // instantiate gesture recognizer

UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(didSwipe:)];
[imageView addGestureRecognizer:rightSwipe];

UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(didSwipe:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:leftSwipe];

//setup images
image1 = [UIImage imageNamed:@"1.png"];
image2 = [UIImage imageNamed:@"2.png"];
imageView.image = image1;

}

-(void)didSwipe : (UISwipeGestureRecognizer *) sender
 {
UISwipeGestureRecognizerDirection direction = sender.direction;
switch (direction) {
    case UISwipeGestureRecognizerDirectionRight:
        imageView.image = image2;
        break;

    case UISwipeGestureRecognizerDirectionLeft :
        imageView.image = image1;
        break;


    default:
        break;
   }
}
4

2 回答 2

0

将您的多个图像存储在 NSArray 中。

如果是,现在检查向右滑动的条件,而不是显示该数组中的图像。

如果向左滑动而不是显示该数组中的上一个图像..

易于使用的循环和增量运算符。

我希望你能明白。

于 2013-10-11T07:01:13.173 回答
0

使用下面的代码,您将获得所需的输出。在以下代码中,您的应用将使用相册照片并根据相册中的图像数量自动设置滚动大小。

-(void)getAllPictures
{


    imageArray=[[NSArray alloc] init];
    mutableArray =[[NSMutableArray alloc]init];
    NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];

    library = [[ALAssetsLibrary alloc] init];

    void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if(result != nil) {
            if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

                NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) {
                             [mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];

                             if ([mutableArray count]==count)
                             {
                                 imageArray=[[NSArray alloc] initWithArray:mutableArray];
                                 [self allPhotosCollected:imageArray];
                             }
                         }
                        failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); } ]; 

            } 
        }
    };

    NSMutableArray *assetGroups = [[NSMutableArray alloc] init];

    void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups addObject:group];
            count=[group numberOfAssets];
        }
    };

    assetGroups = [[NSMutableArray alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAll
                           usingBlock:assetGroupEnumerator
                         failureBlock:^(NSError *error) {NSLog(@"There is an error");}];
}



-(void)allPhotosCollected:(NSArray*)imgArray
{


    CGSize imageSize;

    imageSize.width= imageView.bounds.size.width;
    imageSize.height = imageView.bounds.size.height;
    self.scrollView=[[UIScrollView alloc]init];
    self.scrollView.frame = imageView.frame;
    self.scrollView.contentSize = CGSizeMake(imageSize.width * imgArray.count, imageSize.height);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces=YES;
    self.scrollView.scrollEnabled = YES;
    self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
    [self.scrollView flashScrollIndicators];
    [self.view addSubview:self.scrollView];

    CGFloat xPos = 0.0;

    for (UIImage *image in imgArray) {
            imageView = [[UIImageView alloc] initWithImage:image];
            imageView.frame = CGRectMake(xPos, 0.0, imageSize.width, imageSize.height);


         [self.scrollView addSubview:imageView];

        xPos += imageSize.width;
        // assuming ARC, otherwise release imageView

    }



}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20,320 , 420)];
    [self getAllPictures];
}
于 2013-10-11T06:21:13.070 回答