2015-08-13 18 views
0

與其他應用程序非常相似,我的應用程序有一個「歡迎」頁面控制器,可快速瀏覽功能。在此概述,我畫中的UITabBar一樣,有一個圓形表演,其中的相關功能如下所示:在UIPageController中共享視圖的UIView動畫

Circle over UITabBarItem

我使用每一個頁面繪製時執行以下代碼繪製圓形:

double circleSize = 75; 
[circleView removeFromSuperview]; 
circleView = [[UIView alloc] initWithFrame:CGRectMake(circleX, 
                 circleY, 
                 circleSize, 
                 circleSize)]; 
circleView.layer.cornerRadius = (circleSize/2); 
circleView.layer.borderColor = [UIColor VancityTransitBlue].CGColor; 
circleView.layer.borderWidth = 2; 

我希望這個圓圈看起來像「呼吸」(慢慢地增長,然後縮回原始大小)。我用的是確切的代碼從my answer to this question

[UIView animateWithDuration:1 
         delay:0 
        options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat 
       animations:^{ 
        circleView.transform = CGAffineTransformMakeScale(1.5, 1.5); 
       } 
       completion:nil]; 

這個圈子跨越三個網頁的頁面控制器的共享,並提請就好了。動畫在第一頁上工作得很好,定期在第二頁上工作,並且從不在第三頁上工作。

如何在每個頁面上爲每個圓圈視圖播放動畫?

+0

如果我沒有提供足夠的代碼,或者您想看到其它的代碼,請隨時申請。 –

+0

出於好奇,您如何在每個標籤欄項目中心上方定位'circleX'&'circleY'? – SwiftArchitect

回答

1

這是一個適用於所有選項卡的解決方案。動畫可以在進行中移動。您可能想要使用更好的機制來管理動畫,以便可以高效取消動畫。以下代碼在選項卡控制器中實施。確保-showOverTabBarItem:-hideCircleView在主線程上執行。它已經內置,鏈接,運行測試

enter image description here

顯示

-(void)showOverTabBarItem:(CGFloat)x { 
    [self hideCircleView]; 

    self.circleView.frame =({ 
     CGRect frame = self.circleView.frame; 
     frame.origin.x = x; 
     frame; 
    }); 
    [self.view addSubview:self.circleView]; 

    [UIView animateWithDuration:1 
          delay:0 
         options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat 
        animations:^{ 
         self.circleView.transform = CGAffineTransformMakeScale(1.5, 1.5); 
        } 
        completion:nil]; 
} 

隱藏

- (無效)hideCircleView { [self.circleView removeFromSuperview]; }

初始化

如在viewDidLoad

double circleSize = 75; 
CGFloat circleY = self.view.bounds.size.height-(circleSize/2); 

self.circleView = [[UIView alloc] initWithFrame:CGRectMake(0, 
                  circleY, 
                  circleSize, 
                  circleSize)]; 
self.circleView.layer.cornerRadius = (circleSize/2); 
self.circleView.layer.borderColor = [UIColor blueColor].CGColor; 
self.circleView.layer.borderWidth = 2; 

調用

傳遞的水平位置的動畫:[self showOverTabBarItem: self.view.bounds.size.width/2];

保持周圍

認爲這是關鍵的一步:當你的-removeFromSuperView,要確保該對象不被回收。

@interface TabController() 
@property (nonatomic, retain) UIView * circleView; 
@end 
0

爲什麼每次繪製頁面時都要分配圓圈?

你可以刪除這些行(只需要這些第一INIT):

if (circleView == nil) { 
    circleView = [[UIView alloc] initWithFrame:CGRectMake(circleX, 
                  circleY, 
                  circleSize, 
                  circleSize)]; 
    circleView.layer.cornerRadius = (circleSize/2); 
    circleView.layer.borderColor = [UIColor VancityTransitBlue].CGColor; 
    circleView.layer.borderWidth = 2; 
} 

對於添加到另一頁:從當前頁

[circleView removeFromSuperview]; 
    [circleView.layer removeAllAnimations]; 

刪除,然後將其添加到另一頁:

[self.view addSubView:circleView]; 
[UIView animateWithDuration:1 
         delay:0 
        options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat 
       animations:^{ 
        circleView.transform = CGAffineTransformMakeScale(1.5, 1.5); 
       } 
       completion:nil]; 
+0

錯誤,你是否建議我添加該行?因爲它已經存在了......第一個代碼塊的第二行。 –

+0

@ecnepsnai我編輯了我的答案。你能否展示更多關於[UIView animateWithDuration ...]的細節?例如: - 你在哪裏放置這一行?... – anhtu

+0

這是在同一範圍內第一個代碼塊,我叫'[self.view addSubView:circleView];後立即'。 –