您好我正在使用一个新的应用程序,该应用程序具有从JSON加载数据的CollectionView,当点击特定的Cell时,会转到Detail / FullScreenView。到目前为止一切顺利,但现在我想实施滑动。所以想法是点击/点击特定的Cell - >获取全屏视图 - >滑动FullScreenView上的所有图像。关键是我的全屏视图正在工作并显示先前在CollectionView上选择的图像,但是滑动不起作用,或者它已被识别但未更改图像。一些帮助会很好。提前谢谢!
我的fotos.h(NSObject)
@property NSString* Foto;
@property NSString* Descricao;
@property NSData* FotoImagem;
我的globals.h
+ (NSMutableArray*) getFotos;
+ (void) setFotos: (NSMutableArray*) fotos;
globals.m
+ (NSMutableArray*) getFotos
{
if (_fotos == nil)
_fotos = [NSMutableArray new];
return _fotos;
}
+ (void) setFotos:(NSMutableArray*) fotos
{
_fotos = fotos;
}
CollectionView.h
@property IBOutlet UICollectionView *collectionView;
- (IBAction)back:(id)sender;
CollectionView.m
- (void)loadJSON
{
NSMutableArray* fotos = [[NSMutableArray alloc] init];
NSError *erro;
NSData* jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:URL_json] options:NSDataReadingUncached error:&erro];
NSLog(@"Por alguma razão não consegues carregar os dados!");
NSString* jsonStringData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
if (jsonData == nil) {
}
else {
NSError *erro1;
NSArray *listafotos = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&erro1];
NSInteger totalfotos = [listafotos count];
for (NSDictionary* fot in listafotos){
ExpSFotos* f1 = [ExpSFotos new];
f1.Foto = [fot objectForKey:@"Foto"];
f1.Descricao = [fot objectForKey:@"Descricao"];
NSString* fotoX = f1.Foto;
NSString* urlFoto = [[NSString alloc] initWithFormat:@"%@%@", URL_fotos, fotoX];
NSData* fotoData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlFoto]];
f1.FotoImagem = fotoData;
[fotos addObject:f1];
}
[ExpSGlobals setFotos:fotos];
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSMutableArray *fotos = [ExpSGlobals getFotos];
return fotos.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ExpSCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor clearColor];
if (cell == nil) {
} else {
NSMutableArray *fotos = [ExpSGlobals getFotos];
ExpSFotos *f = [fotos objectAtIndex:indexPath.row];
cell.foto.image = [UIImage imageWithData:f.FotoImagem];
}
return cell;
}
NSIndexPath* lastIndexPath;
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
lastIndexPath = indexPath;
[self performSegueWithIdentifier:@"fullScreen" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"fullScreen"])
{
NSMutableArray* fotos = [ExpSGlobals getFotos];
ExpSFotos* f = [fotos objectAtIndex:lastIndexPath.row];
ExpSFullScreenViewController* fullScreen = (ExpSFullScreenViewController*)segue.destinationViewController;
fullScreen.imageToLoad = [UIImage imageWithData:f.FotoImagem];
fullScreen.detailsDataSource = [[NSArray alloc] initWithArray:fotos];
fullScreen.detailIndex = lastIndexPath.row;
}
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
FullScreen.h
@property UIImage *imageToLoad;
@property IBOutlet UIImageView *full;
@property (nonatomic) NSString *selectedObject;
@property NSMutableArray *arr;
@property (nonatomic) NSArray *detailsDataSource;
@property int detailIndex;
FullScreen.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.full.image = self.imageToLoad;
[full setUserInteractionEnabled:YES];
UISwipeGestureRecognizer *leftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDetectedLeft:)];
leftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:leftGesture];
UISwipeGestureRecognizer *rightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDetectedRight:)];
rightGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:rightGesture];
}
- (void)swipeDetectedRight:(UISwipeGestureRecognizer *)sender
{
if (detailIndex != 0)
detailIndex --;
NSLog(@"Swipe Right Detected");
full.image = [UIImage imageNamed:[[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"Foto"]];
NSLog(@"%@", full);
}
- (void)swipeDetectedLeft:(UISwipeGestureRecognizer *)sender
{
if (detailIndex < [detailsDataSource count])
detailIndex ++;
full.image = [UIImage imageNamed:[[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"Foto"]];
NSLog(@"Swipe Left Detected");
NSLog(@"%@", full);
}
我对这种语言不熟悉,对其他语言也没有太多的了解,但经过一些努力和研究,我已经能够实现我所期待的目标直到现在。一些帮助将不胜感激!