Ios 如何在触摸时旋转图像并将图像拖动到另一图像上
我正在开发一个应用程序,我需要一个画廊,将有许多图像。我想做的是,当用户点击任何图像时,它应该将该图像旋转90度,如果用户将任何图像拖动到任何其他图像的顶部,那么这些图像应该切换位置(或者我们可以说相互交换位置) 说明: A、B、C D E F 假设上面是我的图库的图像,所以如果用户将图像A拖到图像E的顶部,那么图库应该是这样的 E、B、C D A F 我正在使用UICollectionView制作我的图库。以下是我迄今为止所做的工作Ios 如何在触摸时旋转图像并将图像拖动到另一图像上,ios,objective-c,iphone,uiimageview,uicollectionview,Ios,Objective C,Iphone,Uiimageview,Uicollectionview,我正在开发一个应用程序,我需要一个画廊,将有许多图像。我想做的是,当用户点击任何图像时,它应该将该图像旋转90度,如果用户将任何图像拖动到任何其他图像的顶部,那么这些图像应该切换位置(或者我们可以说相互交换位置) 说明: A、B、C D E F 假设上面是我的图库的图像,所以如果用户将图像A拖到图像E的顶部,那么图库应该是这样的 E、B、C D A F 我正在使用UICollectionView制作我的图库。以下是我迄今为止所做的工作 - (void)viewDidLoad { [sup
- (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);
}
但我认为CGAffineTransformMakeRotation在我的情况下不起作用
就旋转而言,以下是实现此目的的方法:
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];*/
cGraffineTransformMakeRotation
参数是角度,允许您以所需的方式旋转。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到indexPath:(NSIndexPath*)newIndexPath
根据这一点,它是用来处理UI操作的方法
使用此方法重新组织现有数据项。你可以这样做
在数据源对象内或中重新排列项目时
对用户与集合视图交互的响应
我看到您已经编辑了这一行
cell.imageView.transform=cgafinetransformconcat(cell.view.transform,rotation)
但是我的customCell没有任何名为view的对象,所以我尝试了这个cell.imageView.transform=CGAffineTransformConcat(cell.imageView.transform,rotation)代码>但还是不走运。你是对的,我的错,我编辑了!该行为cell.imageView.transform=CGAffineTransformConcat(cell.transform,rotation)代码>我自己做了一些测试,效果很好代码>按ClosetsCell*单元格=(ClosetsCell*)[collectionView cellForItemAtIndexPath:indexPath]代码>。由于单元格已经可见,您可以使用cellForItem
获得它,无需实例化新的单元格!很有效,谢谢!!它现在可以工作了,我已经对你的答案投了赞成票,但是有一个问题,我们应该如何调整它,以便每次我点击图像时它都可以旋转。现在它只旋转一次。你想让它旋转回原来的状态吗?