【问题标题】:Control UIImageview animation flow with touch events使用触摸事件控制 UIImageview 动画流
【发布时间】:2013-02-06 05:15:25
【问题描述】:

我正在使用 UIImageView 通过动画 .png 图像来模拟 3D。是否可以使用触摸事件来控制动画的流程?例如,如果我正在模拟一个盒子,我可以用手指向左还是向右旋转它?如果没有,你能指点我另一个方向吗?

【问题讨论】:

    标签: cocoa-touch animation uiimageview png


    【解决方案1】:

    首先有两种方法可以做到这一点,最难的方法是使用核心动画构建立方体并在滑动或触摸时旋转它,第二种(最简单的)是添加 .png 序列动画到由 UISwipeGestureRecogniser 触发的 imageView。

    首先创建您的手势识别器,通常只需放入 ViewDidLoad

     //Implement Swipe Views
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft)];
    swipeRight.numberOfTouchesRequired = 1;
    swipeRight.direction = UISwipeGestureRecognizerDirectionLeft;
    [_imageView addGestureRecognizer:swipeRight];
    

    然后实现您在 @selector(swipeRight) 中声明的 swipeRight 方法,以及您希望在滑动时播放的动画...

    -(void)swipeRight
    {
    NSMutableArray *animationArray = [[NSMutableArray alloc] init];
    
        int animationSizeTwo = 30;
    
        for (int i=0; i< animationSize; i++) {
    
            NSString *zeros = @"000";
    
            if ( i >= 1000 ) {
    
                zeros = @"";
    
            }
            else if ( i >= 100 ) {
    
                zeros = @"0";
    
            }
            else if ( i >= 10 ) {
    
                zeros = @"00";
            }
            NSString *animationNameTwo = [NSString stringWithFormat:@"(imageName)"];
    
            NSString *imageName = [NSString stringWithFormat:@"%@%@%d.png", animationNameTwo, zeros, i];
    
            [animationArrayTwo addObject:[UIImage imageNamed:imageName]];
    
    
        }
    
        _imageView.animationImages = animationArray;
        _imageView.animationDuration = 1;
        _imageView.animationRepeatCount = 1;
        _imageView.image = [_imageView.animationImages lastObject];
        [_imageView startAnimating];
    
    }
    

    现在,只要您向右滑动,动画就会触发。您可以复制和粘贴 UIGestureRecogniser 代码并更改所有方法,这样您就可以双向触发。当您实现 swipeLeft 时,只需将动画序列计数更改为:

    int animationSizeTwo = 0;
    
        for (int i=30; i> animationSize; i--) {
    

    这将使您的动画反向旋转。

    希望这对您有所帮助,让我知道您的进展情况!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-19
      • 2013-07-22
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多