您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 360 度旋轉效果 Demo 實作

編輯:iOS 開發綜合

本 Demo 示範如何用 UIImageView 實現 360 度旋轉效果。經在 iPhone 6.1 上測試成功,主要程式碼位於 FVImageSequence.m 中。

核心原理其實很直觀:在 touchesMoved 事件中,根據手指移動的方向,動態替換 UIImageView 的 image,藉由連續切換不同角度拍攝的圖片,達到旋轉的視覺效果。

Demo 截圖:

主要程式碼如下:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    [super touchesMoved:touches withEvent:event];

   

    UITouch *touch = [[event allTouches] anyObject];

    CGPoint touchLocation = [touch locationInView:self];

   

    int location = touchLocation.x;

    //根據移動偏移量,判斷旋轉的方向。

   

    if(location < previous)

        current += increment;

    else

        current -= increment;

   

    previous = location;

   

    //圖片臨界點限制,在 demo 的 image 文件夾下有所有圖片,是從不同角度拍攝的,將這些圖片依次顯示就可以實現 360 的旋轉效果

    if(current > numberOfImages)

        current = 0;

    if(current < 0)

        current = numberOfImages;

   

    NSString *path = [NSString stringWithFormat:@"%@%d", prefix, current];

    NSLog(@"%@", path);

   

    path = [[NSBundle mainBundle] pathForResource:path ofType:extension];

   

    UIImage *img = [[UIImage alloc] initWithContentsOfFile:path];

   

    //設置 UIImageView 的 image 為新的 image,實現 360 度旋轉效果

    [self setImage:img];

   

    [img release];

透過手指左右滑動改變 current 索引,再依序載入對應角度的圖片,即可流暢呈現如實物般的 360 度旋轉體驗。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved