2014-09-12 2 views
3

많은 이미지가 포함 된 갤러리가 필요한 앱을 개발 중입니다. 그리고 내가하고 싶은 일은 사용자가 이미지를 90도 회전해야하는 이미지를 탭/클릭하고 사용자가 다른 이미지의 상단으로 이미지를 드래그하면 해당 이미지가 해당 위치로 전환되어야합니다 (또는 스왑 서로의 장소).터치시 이미지 회전 및 다른 이미지 위로 이미지 드래그

설명 : 이미지 E의 상단에 사용자가 드래그 이미지 A가 다음 갤러리이

EBC과 같아야합니다 그래서 만약 DEF

위의 가정

ABC

내 갤러리의 이미지입니다

DAF

내 갤러리를 만들기 위해 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.

답변

3

는 지금까지의 회전에 관한 한, 여기 당신이 이것을 달성 할 수있는 방법은 다음과 같습니다

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 작업을 처리하는 데 사용하는 방법입니다.

기존 데이터 항목을 재구성하려면이 방법을 사용하십시오. 데이터 소스 개체 내의 항목을 다시 정렬 할 때 을 수행하거나 컬렉션보기와의 사용자 상호 작용에 대한 응답으로 을 수행 할 수 있습니다.

+0

'cell.imageView.transform = CGAffineTransformConcat (cell.view.transform, rotation);'이 줄을 편집 한 것을 볼 수 있습니다. 그러나 customCell에는 view라는 객체가 없으므로이 셀을 시도했습니다. .imageView.transform = CGAffineTransformConcat (cell.imageView.transform, rotation);'그러나 여전히 행운이 없습니다. – Zac24

+0

당신이 옳았 어, 편집 했어! 그 줄은'cell.imageView.transform = CGAffineTransformConcat (cell.transform, rotation); '입니다. 저는 독자적으로 몇 가지 테스트를했는데 작동합니다. – Starscream

+0

'ClosetsCell * cell = (ClosetsCell *) [collectionView cellForItemAtIndexPath : indexPath];'에 의해'ClosetsCell * cell = (ClosetsCell *) [collectionView dequeueReusableCellWithReuseIdentifier : cellIdentifier forIndexPath : indexPath];를 대체하십시오. 셀이 이미 보이기 때문에,'cellForItem'으로 가져올 수 있습니다. 새로운 것을 instanciate 할 필요가 없습니다! 그것은 작동합니다. – Starscream