2011-12-28 19 views
0

私はローディングビューのUIViewを持っています。それがするすべては、円形のローディングサークル(1つの文で多くの「円」に変わります)を表示します。
初めてうまく動作しますが、その後は円が中央に配置されません。左右に動く。どのように私はそれが常に中心になるようにすることができますが、私はすべてのビューで横長のモード(風景の左、風景の権利)で表示するだけのアプリを制限しているので、問題は回転されているデバイスから来ていないことに注意してください。2番目の起動時にローディング画面が中央に表示されない

ビューをロードする

コール:

loadingViewController = [LoadingViewController loadSpinnerIntoView:self.view]; 

LoadingViewController.h:

#import <UIKit/UIKit.h> 
#import <QuartzCore/QuartzCore.h> 
#import "CrestronClient.h" 
@interface LoadingViewController : UIView 
{ 
    CrestronClient *cClient; 


} 
+(LoadingViewController *)loadSpinnerIntoView:(UIView *)superView; 
-(void)removeLoadingView; 
- (UIImage *)addBackground; 
@end 

LoadingView.m:

#import "LoadingViewController.h" 
#import "RootViewController.h" 
@implementation LoadingViewController 

CGRect priorFrameSettings; 
UIView *parentView; 


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 

    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||interfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
     return YES; 
    }else{ 
     return NO; 
    } 

} 


-(void)removeLoadingView 
{ 

// [parentView setFrame:priorFrameSettings]; 

    CATransition *animation = [CATransition animation]; 

    [animation setType:kCATransitionFade]; 

    [[[self superview] layer] addAnimation:animation forKey:@"layerAnimation"]; 

    [self removeFromSuperview]; 
} 

+(LoadingViewController *)loadSpinnerIntoView:(UIView *)superView 
{ 

    priorFrameSettings = superView.frame; 
    parentView = superView; 
    // [superView setFrame:CGRectMake(0, 0, 1024, 1024)]; 


    // Create a new view with the same frame size as the superView 


LoadingViewController *loadingViewController = [[LoadingViewController alloc] initWithFrame:superView.frame]; 

loadingViewController.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

    // If something's gone wrong, abort! 

    if(!loadingViewController){ return nil; } 


    [superView addSubview:loadingViewController]; 

    if(!loadingViewController){ return nil; } 

    // This is the new stuff here ;) 

    UIActivityIndicatorView *indicator = 

    [[[UIActivityIndicatorView alloc] 

     initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge] autorelease]; 

    // Set the resizing mask so it's not stretched 
    UIImageView *background = [[UIImageView alloc] initWithImage:[loadingViewController addBackground]]; 

    // Make a little bit of the superView show through 

    background.alpha = 0.7; 

    [loadingViewController addSubview:background]; 

    indicator.autoresizingMask = 

    UIViewAutoresizingFlexibleTopMargin | 

    UIViewAutoresizingFlexibleRightMargin | 

    UIViewAutoresizingFlexibleBottomMargin | 

    UIViewAutoresizingFlexibleLeftMargin; 

    // Place it in the middle of the view 

    indicator.center = superView.center; 

    // Add it into the spinnerView 

    [loadingViewController addSubview:indicator]; 

    // Start it spinning! Don't miss this step 

    [indicator startAnimating]; 


    // Create a new animation 

    CATransition *animation = [CATransition animation]; 

    // Set the type to a nice wee fade 

    [animation setType:kCATransitionFade]; 

    // Add it to the superView 

    [[superView layer] addAnimation:animation forKey:@"layerAnimation"]; 


    return loadingViewController; 

} 
- (UIImage *)addBackground{ 

    cClient = [CrestronClient sharedManager]; 
    if (cClient.isConnected == FALSE) { 
     [cClient connect]; 
    } 
    // Create an image context (think of this as a canvas for our masterpiece) the same size as the view 

    UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 1); 

    // Our gradient only has two locations - start and finish. More complex gradients might have more colours 

    size_t num_locations = 2; 

    // The location of the colors is at the start and end 

    CGFloat locations[2] = { 0.0, 1.0 }; 

    // These are the colors! That's two RBGA values 

    CGFloat components[8] = { 

     0.4,0.4,0.4, 0.8, 

     0.1,0.1,0.1, 0.5 }; 

    // Create a color space 

    CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB(); 

    // Create a gradient with the values we've set up 

    CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations); 

    // Set the radius to a nice size, 80% of the width. You can adjust this 

    float myRadius = (self.bounds.size.width*.8)/2; 

    // Now we draw the gradient into the context. Think painting onto the canvas 

    CGContextDrawRadialGradient (UIGraphicsGetCurrentContext(), myGradient, self.center, 0, self.center, myRadius, kCGGradientDrawsAfterEndLocation); 

    // Rip the 'canvas' into a UIImage object 

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

    // And release memory 

    CGColorSpaceRelease(myColorspace); 

    CGGradientRelease(myGradient); 

    UIGraphicsEndImageContext(); 

    // … obvious. 

    return image; 

} 
- (void)dealloc { 

    [super dealloc]; 
} 
@end 
+0

ローテーションメッセージやローディングメッセージ( '-viewDidLoad'、' -shouldAutorotate ... 'など)があります。 –

+0

私は警告からこれを気付いたが、viewdidloadが呼び出され、それを使用しているので、私はそれを残していると考えた。 –

答えて

0

[background setFrame:CGRectMake(0, 0, 1024, 768)]; 

を追加することで、背景を固定しての円のセンタリングを固定:それはあなたのアクティビティビューがオフセンターであることに関連していないですが、UIViewのサブクラスが送信されない

indicator.center = background.center; 
0

のロードビューがその親フレームに設定されていることを確認します適切なautoresizingMaskが設定されています。これはおそらくUIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeightによって起こります。

+0

親にフレームを設定するには、最初の2回目バックグラウンドで見ると、画面の四分の一が右側に表示されます。私はすでにサイズ変更用のマスクを適用しているので、特に自動サイズ変更の方法を行う方法はわかりません。私はマスクをやっている間違った方法がありますか? –

+0

あなたの例では 'indicator'の' autoresizingMask'を設定していますが、 'loadingViewController'は設定していません。 'loadingViewController'を設定してみてください。 – Joe

+0

loadViewController.autoresizingMask = UIViewAutoresizingFlexibleWidthを試してください。 UIViewAutoresizingFlexibleHeight; –

関連する問題