2016-01-23 80 views
-1

我想創建自定義活動指示器它有兩個圖像一個圖像將靜態(在後臺),另一個圖像將在前面,它必須移動左,右,上,下(只是爲了給動畫感覺)。我試圖使用下面的代碼(其錯誤的一個)。任何人都可以幫助我,如何做動畫iOS:自定義活動指示器

UIImage *statusImage = [UIImage imageNamed:@"image1.png"]; 
UIImageView *activityImageView = [[UIImageView alloc] 
            initWithImage:statusImage]; 


//Add more images which will be used for the animation 
activityImageView.animationImages = [NSArray arrayWithObjects: 
            [UIImage imageNamed:@"image1.png"], 
            [UIImage imageNamed:@"image2.png"], nil]; 


//Set the duration of the animation (play with it 
//until it looks nice for you) 
activityImageView.animationDuration = 7; 

activityImageView.frame = CGRectMake(
            self.view.frame.size.width/2 
            -statusImage.size.width/3, 
            self.view.frame.size.height/2 
            -statusImage.size.height/3, 
            statusImage.size.width/3, 
            statusImage.size.height/3); 

//Start the animation 
[activityImageView startAnimating]; 
+0

PLZ不與圖像發起的ImageView '[[UIImageView的頁頭] initWithImage:statusImage]' 嘗試與框架或僅初始化啓動。 –

+0

@RatulSharker爲什麼不呢?你想說什麼? –

回答

0

UIImageView可以通過圖像動畫,有點像一個gif。使用CAAnimation可以更加優雅地實現你想要的東西。此代碼給你展示了一個動畫,左,右:

- (CATransform3D) moveToRight 
{ 
    return [self moveWithScale:CGSizeMake(0.25f, 1.5f)]; 
} 
- (CATransform3D) moveToLeft 
{ 
    return [self moveWithScale: CGSizeMake(-0.25f, 1.5f)]; 
} 
- (CATransform3D) moveWithScale: (CGSize) scale 
{ 
    CGAffineTransform scaleTransform CGAffineTransformMakeScale(ABS(scale.width), scale.height); 
    CGFloat XTranslationFactor = (1.0f - ABS(scale.width))/2.0 * (ABS(scale.width)/scale.width); 

    CGAffineTransform translationTransform = CGAffineTransformMakeTranslation(XTranslationFactor * self.bounds.size.width, 
                      0.0f); 
    CGAffineTransform completeTransform = CGAffineTransformConcat(scaleTransform, translationTransform); 

    CATransform3D transform3D = CATransform3DMakeAffineTransform(completeTransform); 
    return transform3D; 
} 
- (NSArray *) keyFrameAnimationValues 
{ 
    values = @[[NSValue valueWithCATransform3D: [self moveToLeft]], 
       [NSValue valueWithCATransform3D: [self moveToRight]], 
       [NSValue valueWithCATransform3D: [self moveToTop]], 
       [NSValue valueWithCATransform3D: [self moveToBottle]] 
       ]; 
} 

- (void) startAnimating 
{ 
    NSArray * values = [self keyFrameAnimationValues]; 
    NSInteger frameCount = frameValues.count; 
    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath: @"transform"]; 
    keyFrameAnimation.duration = 7; 
    keyFrameAnimation.calculationMode = kCAAnimationCubic; 
    keyFrameAnimation.values = [self keyFrameAnimationValues]; 
    // this next part could be done a little more generic 
    // by constructing the array dynamically using a for loop, 
    // but this will give you an idea 
    keyFrameAnimation.keyTimes = @[@(1.0f/frameCount), 
            @(2.0f/frameCount), 
            @(3.0f/frameCount), 
            @(4.0f/frameCount) 
           ]; 
    keyFrameAnimation.repeatCount = HUGE_VALF; 
    [self.layer addAnimation: keyFrameAnimation forKey: @"myAnimation"]; 
} 
+0

此解決方案看起來可行,我會檢查並找回給您 –

0

創建要在每個角落移動,並將其添加爲定位對象層CAAnimation。希望它可以幫助