我正在开发一个应用程序,我需要一个包含许多图像的图库。我想要做的是,当用户点击/单击任何图像时,它应该将该图像旋转到 90 度,如果用户将任何图像拖动到任何其他图像的顶部,那么这些图像应该交换它们的位置(或者我们可以说交换)彼此的位置)。
解释:
A B C
D E F
假设上面是我的画廊的图像,所以如果用户将图像 A 拖动到图像 E 的顶部,那么画廊应该如下所示
E B C
D A F
我正在使用 UICollectionView 来制作我的画廊。这是我到目前为止所做的。
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageArray = [NSArray arrayWithObjects:@"angry_birds_cake.jpg", @"creme_brelee.jpg", @"egg_benedict.jpg", @"full_breakfast.jpg", @"green_tea.jpg", @"ham_and_cheese_panini.jpg", nil];
[self.collectionView registerClass:[ClosetsCell class] forCellWithReuseIdentifier:@"ClosetsCell"];
[self.collectionView setPagingEnabled:YES];
// Configure layout
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(100, 100)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[self.collectionView setCollectionViewLayout:flowLayout];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.imageArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// Setup cell identifier
static NSString *cellIdentifier = @"ClosetsCell";
ClosetsCell *cell = (ClosetsCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:[self.imageArray objectAtIndex:indexPath.row]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]];
// Return the cell
return cell;
}
-(CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 1;
}
-(CGFloat) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 4;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"ClosetsCell";
ClosetsCell *cell = (ClosetsCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
printf("Selected View index=%d",indexPath.row);
cell.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
}
但我不认为 CGAffineTransform MakeRotation 在我的案例中起作用。
就旋转而言,实现此目的的方法如下:
CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI / 2.0);
/* If you want an animation
[UIView beginAnimations: @"" context: NULL];
[UIView setAnimationDuration: 0.3];
*/
cell.imageView.transform = CGAffineTransformConcat(cell.transform, rotation);
/*[UIView commitAnimations];*/
The CGAffineTransformMakeRotation
参数是角度,可以让你按照你想要的方式旋转。 M_PI / 2.0 表示左旋转,M_PI / -2.0 右旋转,角度 0 将使您返回纵向。如果您的实现想要在单击单元格时来回旋转,您可以实现如下所示:
CGAffineTransform leftRotation = CGAffineTransformMakeRotation(M_PI / 2.0);
CGAffineTransform portraitRotation = CGAffineTransformMakeRotation(0.0);
if (CGAffineTransformEqualToTransform(leftRotation, cell.imageView.transform)){
cell.imageView.transform = CGAffineTransformConcat(cell.transform, portraitRotation);
}
else if (CGAffineTransformEqualToTransform(portraitRotation, cell.imageView.transform)){
cell.imageView.transform = CGAffineTransformConcat(cell.transform, leftRotation);
}
现在对于你的第二个问题,切换单元格,我从未使用过 UICollectionViews 但我认为你应该研究一下- (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
当拖动结束时(通过检查拖动开始和拖动结束的索引路径)。
根据苹果文档 https://developer.apple.com/library/ios/documentation/uikit/reference/UICollectionView_class/Reference/Reference.html#//apple_ref/occ/instm/UICollectionView/moveItemAtIndexPath:toIndexPath:它是用来处理 UI 操作的方法。
使用此方法重新组织现有数据项。你可能会这样做
当您重新排列数据源对象中的项目或
响应用户与集合视图的交互。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)