gpt4 book ai didi

ios - 如何在触摸时旋转图像并将图像拖到另一个图像上

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:59:49 24 4
gpt4 key购买 nike

我正在开发一个应用程序,我需要一个包含许多图像的图库。我想要做的是,当用户点击/单击任何图像时,它应该将该图像旋转 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);

}

但我认为 CGAffineTransformMakeRotation 不适用于我的案例。

This is how my gallery looks.

最佳答案

就轮换而言,以下是实现此目的的方法:

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];*/

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当拖动结束时(通过检查拖动开始和拖动结束的 indexPath)。

根据 Apple documentation这是用于处理 UI 操作的方法。

Use this method to reorganize existing data items. You might do this when you rearrange the items within your data source object or in response to user interactions with the collection view.

关于ios - 如何在触摸时旋转图像并将图像拖到另一个图像上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25803120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com