2011-12-28 67 views
0

我有一个UIView这是我的加载视图。它所做的只是显示循环加载循环(对于一个句子,大部分“圆圈”都是lol)。
它第一次正常工作,但之后,圆不居中。它向左和向下移动一些。我怎样才能让它始终居中,请记住,我限制了应用程序仅在所有视图中以横向模式显示(横向左侧,横向右侧),因此问题不是来自正在旋转的设备。加载屏幕没有集中在第二次启动

调用加载视图:

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

这是不相关的活动看法是偏离中心,但UIView的子类不发送旋转和加载消息(例如'-viewDidLoad','-shaouldAutorotate ...'等)。 – 2011-12-28 19:53:51

+0

我注意到这个警告,但viewdidload确实被调用,即时通讯使用它,所以我想我会离开它。 – 2011-12-28 21:12:27

回答

0

加入

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

固定的背景和固定的圆的中心有:

indicator.center = background.center; 
0

确保装载视图设置为其父框架并设置了适当的autoresizingMask。这可能是由UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight

+0

设置框架给父母我有同样的问题,它的第一次工作现在第二次运行尽管透视背景留下了右侧四分之一的屏幕。我不知道如何做自动调整大小的东西,特别是因为我已经应用了调整大小的面具。我在做面具的方式有什么问题吗? – 2011-12-28 19:47:07

+0

您在示例中设置了'indicator'的'autoresizingMask',但不是'loadingViewController'。尝试设置'loadingViewController'。 – Joe 2011-12-28 19:53:01

+0

尝试loading loadingController.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; – 2011-12-28 19:54:51

相关问题