1

我想创建像http://disqus.com/这样的背景继续背景,但尝试使用我的代码后我无法获得它

continuesBackground =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pattern"]];
    continuesBackground.frame=CGRectMake(0, 56, 320, 102);
    continuesBackground.layer.zPosition=100;
    [UIView animateWithDuration:5.0
                          delay:0.0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         continuesBackground.transform = CGAffineTransformMakeTranslation(0, 200);  }
                     completion:^(BOOL finish) {
                         continuesBackground.transform = CGAffineTransformMakeTranslation(200, 0);

                     }];

怎么做?我需要 nstimer 吗?

4

3 回答 3

4

试试这个:

- (void)animateImageViewFromStartPosition:(CGPoint)aStartPosition toEndPosition:(CGPoint)aEndPosition; {
  CGRect oldFrame = CGRectMake(aStartPosition.x, aStartPosition.y, continuesBackground.frame.size.width, continuesBackground.frame.size.height);
  [continuesBackground setFrame:oldFrame];

  [UIView animateWithDuration:5.0f
                        delay:0.0f
                      options:UIViewAnimationOptionCurveEaseInOut
                   animations:^(void){
                     CGRect newFrame = continuesBackground.frame;
                     newFrame.origin = aEndPosition;
                     continuesBackground.frame = newFrame;
                   }
                   completion:^(BOOL finished){
                     if(finished){
                       [self animateImageViewFromStartPosition:aStartPosition toEndPosition:aEndPosition];
                     }
                      }];
}

然后在你的方法中调用viewDidLoad方法:

- (void)viewDidLoad; {
  [super viewDidLoad];
  [self animateImageViewFromStartPosition:CGPointMake(0.0f, 56.0f) toEndPosition:CGPointMake(320.0f, 56.0f)];
}

您可以轻松地将其修改为变换或任何您想要的,它将使动画无限期地进行。希望有帮助!

于 2012-11-10T05:58:11.377 回答
1
  - (void)animateImageViewFromStartPosition:(CGPoint)aStartPosition toEndPosition:(CGPoint)aEndPosition; {
   CGRect oldFrame = CGRectMake(aStartPosition.x, aStartPosition.y,continuesBackground.frame.size.width, continuesBackground.frame.size.height);
    [continuesBackground setFrame:oldFrame];

     [UIView animateWithDuration:5.0f
                    delay:0.0f
                  options:UIViewAnimationOptionCurveEaseInOut
               animations:^(void){
                 CGRect newFrame = continuesBackground.frame;
                 newFrame.origin = aEndPosition;
                 continuesBackground.frame = newFrame;
               }
               completion:^(BOOL finished){
                 if(finished){
                   [self animateImageViewFromStartPosition:aStartPosition toEndPosition:aEndPosition];
                 }
                  }];
 }

然后在您的 viewDidLoad 方法中调用该方法:

  - (void)viewDidLoad; {
   [super viewDidLoad];
   [self animateImageViewFromStartPosition:CGPointMake(0.0f, 56.0f) toEndPosition:CGPointMake(320.0f, 56.0f)];
   }

谈论此代码,请让我知道在哪里申请代码

于 2012-11-12T17:43:29.437 回答
0

以下是 Swift 4.2 版本中问题的正确答案:

func animateImageViewFrom(startPosition aStartPosition: CGPoint, toEndPosition aEndPosition: CGPoint) {
        let oldFrame = CGRect(x: aStartPosition.x, y: aStartPosition.y, width: continuesBackground.frame.size.width, height: continuesBackground.frame.size.height)
        continuesBackground.frame = oldFrame

        UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseInOut, animations: {
            var newFrame = self.continuesBackground.frame
            newFrame.origin = aEndPosition
            self. continuesBackground.frame = newFrame
        }) { (finished) in
            if finished {
                self.animateImageViewFrom(startPosition: aStartPosition, toEndPosition: aEndPosition)
            }
        }
}
于 2019-04-27T07:43:26.153 回答