当我旋转模拟器时,UIWindow周围有许多黑色块

时间:2015-12-17 12:08:55

标签: ios uiscreen

如何让黑色块消失?

旋转后,黑色块消失。

screenshot

AppDelegate源文件:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *viewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"ViewController"];

    self.window.rootViewController = viewController;

    bar = [KodUserCenterBar instance];

    [self.window makeKeyAndVisible];
    return YES;
}

标题文件:

@interface KodUserCenterBar : UIWindow

/**获取单例
 */
+ (instancetype)instance;

/**显示工具栏
 */
+ (void)show;

/**隐藏工具栏
 */
+ (void)hide;

/**显示功能视图
 */
+ (void)showFunctionView;

/**隐藏功能视图
 */
+ (void)hideFunctionView;

@end

源文件:

#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height



#define kScreenWidth [UIScreen mainScreen].bounds.size.width
 #define kScreenHeight [UIScreen mainScreen].bounds.size.height
 #define kBarWidth 200
 #define kBarImageViewTag 100
 #define kBarItemGap 5
 #define degreesToRadian(x) (M_PI * (x) / 180.0)

 @interface KodUserCenterBarFunctionItemObject : NSObject

 @property (nonatomic, copy) NSString *title;
 @property (nonatomic, assign) NSUInteger indexOfTabBar;
 @property (nonatomic, copy) NSString *imageName;

 + (instancetype)objectWithTitle:(NSString *)title indexOfTabBar:(NSUInteger)index imageName:(NSString *)imageName;

@end

@implementation KodUserCenterBarFunctionItemObject

+ (instancetype)objectWithTitle:(NSString *)title indexOfTabBar:(NSUInteger)index imageName:(NSString *)imageName
{
    KodUserCenterBarFunctionItemObject *obj = [KodUserCenterBarFunctionItemObject new];
    if (obj) {
        obj.title = title;
        obj.indexOfTabBar = index;
        obj.imageName = imageName;
    }
    return obj;
}

@end


@interface KodUserCenterBar()
{
    UIPanGestureRecognizer *pan;
    UIViewController *_rootVC;
}

@property (nonatomic, copy) NSArray *itemsOfBar;
@property (nonatomic, assign) CGFloat initWidth;
@property (atomic, strong) UIButton *button;

@end

static KodUserCenterBar *static_instance = nil;
static BOOL isShowMenu = NO;

@implementation KodUserCenterBar

+ (instancetype)instance
{
    if (static_instance == nil) {
        static_instance = [KodUserCenterBar new];
        [static_instance addTopButton];
    }
    return static_instance;
}

+ (void)show
{
    [KodUserCenterBar instance].hidden = NO;
}

+ (void)hide
{
    [KodUserCenterBar instance].hidden = YES;
}

+ (void)showFunctionView
{
    if (!isShowMenu) {
        [[KodUserCenterBar instance] clickBar:nil];
    }
}

+ (void)hideFunctionView
{
    if (isShowMenu) {
        [[KodUserCenterBar instance] clickBar:nil];
    }
}

- (instancetype)init
{
    if (self = [super init]) {
        self.backgroundColor = [UIColor clearColor];
        self.windowLevel = UIWindowLevelAlert;
        self.userInteractionEnabled = YES;
        [self makeKeyAndVisible];
        self.sd_x = 0;
        self.sd_center_y = [UIScreen mainScreen].bounds.size.height / 2;

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onDeviceOrientationChange) name:UIDeviceOrientationDidChangeNotification object:nil];
    }
    return self;
}

- (NSArray *)itemsOfBar
{
    if (_itemsOfBar == nil) {
        KodUserCenterBarFunctionItemObject *item1 = [KodUserCenterBarFunctionItemObject objectWithTitle:@"个人中心" indexOfTabBar:0 imageName:@"tb3_toolbaruser"];

        KodUserCenterBarFunctionItemObject *item2 = [KodUserCenterBarFunctionItemObject objectWithTitle:@"游戏礼包" indexOfTabBar:1 imageName:@"tb3_toolbargame"];

        KodUserCenterBarFunctionItemObject *item3 = [KodUserCenterBarFunctionItemObject objectWithTitle:@"系统公告" indexOfTabBar:2 imageName:@"tb3_toolbarmsg"];

        KodUserCenterBarFunctionItemObject *item4 = [KodUserCenterBarFunctionItemObject objectWithTitle:@"游戏论坛" indexOfTabBar:3 imageName:@"tb3_toolbarbbs"];

        NSArray *data = @[item1, item2, item3, item4];
        _itemsOfBar = [NSArray arrayWithArray:data];
    }

    return _itemsOfBar;
}

- (void)onDeviceOrientationChange
{
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;

    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
        NSLog(@"is portrait");
        NSLog(@"width = %f, height = %f", kScreenWidth, kScreenHeight);
        [self adjustWindowPosition];
        self.sd_center_y = kScreenHeight / 2;
    }
    else if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {
        NSLog(@"is landscape");
        NSLog(@"width = %f, height = %f", kScreenWidth, kScreenHeight);
        [self adjustWindowPosition];
        self.sd_center_y = kScreenHeight / 2;
    }
}

- (void)addTopButton
{
    pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragBar:)];
    pan.delaysTouchesBegan = NO;
    [self addGestureRecognizer:pan];

    UIViewController *vc = [[KodBasicViewController alloc] init];
    vc.view.backgroundColor = [UIColor clearColor];
    self.rootViewController = vc;
    CGSize size = [UIImage imageNamed:@"tb3_assi_nor"].size;
    self.sd_width = size.width;
    self.sd_height = size.height;
    self.initWidth = size.width;

    _button = [[UIButton alloc] init];
    [_button setBackgroundImage:[UIImage imageNamed:@"tb3_assi_nor"] forState:UIControlStateNormal];
    [_button setBackgroundImage:[UIImage imageNamed:@"tb3_assi_pre"] forState:UIControlStateHighlighted];
    [_button setBackgroundImage:[UIImage imageNamed:@"tb3_assi_pre"] forState:UIControlStateSelected];
    _button.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [_button addTarget:self action:@selector(clickBar:) forControlEvents:UIControlEventTouchUpInside];
    _button.userInteractionEnabled = YES;
    _button.selected = NO;
    [_button becomeFirstResponder];
    [vc.view addSubview:_button];
}

- (void)clickBar:(id)button
{
    NSTimeInterval interval = 0.2;
    __weak typeof(_button) weakButton = _button;

    isShowMenu = !isShowMenu;
    pan.enabled = !isShowMenu;

    if (self.sd_center_x > kScreenWidth / 2) {
        [UIView animateWithDuration:interval animations:^{
            self.sd_x = kScreenWidth;
        } completion:^(BOOL finished) {
            if (isShowMenu) {
                self.sd_width = kBarWidth + self.initWidth;
                _button.sd_right_x = self.sd_width;

                UIImage *image = [UIImage imageNamed:@"tb3_assi_bor_right"];
                image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(1, 30, 1, 0)];
                UIImageView *rightImageView = [[UIImageView alloc] initWithImage:image];
                rightImageView.image = image;
                rightImageView.sd_width = kBarWidth;
                rightImageView.sd_right_x = _button.sd_x;
                rightImageView.sd_center_y = _button.sd_center_y;
                rightImageView.tag = kBarImageViewTag;
                rightImageView.userInteractionEnabled = YES;
                [self.rootViewController.view addSubview:rightImageView];
                self.sd_x = kScreenWidth;

                [weakButton setBackgroundImage:[UIImage imageNamed:@"tb3_assi_act_right"] forState:UIControlStateNormal];
                [weakButton setBackgroundImage:[UIImage imageNamed:@"tb3_assi_act_pre_right"] forState:UIControlStateSelected];
                [weakButton setBackgroundImage:[UIImage imageNamed:@"tb3_assi_act_pre_right"] forState:UIControlStateHighlighted];
                [self addFunctionItems:NO];
            }
            else {
                self.sd_width = self.initWidth;
                _button.sd_right_x = self.sd_width;
                UIView *contentView = [self.rootViewController.view viewWithTag:kBarImageViewTag];
                [contentView removeFromSuperview];

                [weakButton setBackgroundImage:[UIImage imageNamed:@"tb3_assi_nor"] forState:UIControlStateNormal];
                [weakButton setBackgroundImage:[UIImage imageNamed:@"tb3_assi_pre"] forState:UIControlStateSelected];
                [weakButton setBackgroundImage:[UIImage imageNamed:@"tb3_assi_pre"] forState:UIControlStateHighlighted];
            }

            [UIView animateWithDuration:interval animations:^{
                self.sd_right_x = kScreenWidth;
            }];
        }];
    }
    else {
        [UIView animateWithDuration:interval animations:^{
            self.sd_right_x = 0;
        } completion:^(BOOL finished) {
            if (isShowMenu) {
                self.sd_width = kBarWidth + self.initWidth;

                UIImage *image = [UIImage imageNamed:@"tb3_assi_bor_left"];
                image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(1, 0, 1, 30)];
                UIImageView *leftImageView = [[UIImageView alloc] initWithImage:image];
                leftImageView.image = image;
                leftImageView.sd_width = kBarWidth;
                leftImageView.sd_x = _button.sd_right_x;
                leftImageView.sd_center_y = _button.sd_center_y;
                leftImageView.tag = kBarImageViewTag;
                leftImageView.userInteractionEnabled = YES;
                [self.rootViewController.view addSubview:leftImageView];
                self.sd_right_x = 0;

                [weakButton setBackgroundImage:[UIImage imageNamed:@"tb3_assi_act_left"] forState:UIControlStateNormal];
                [weakButton setBackgroundImage:[UIImage imageNamed:@"tb3_assi_act_pre_left"] forState:UIControlStateSelected];
                [weakButton setBackgroundImage:[UIImage imageNamed:@"tb3_assi_act_pre_left"] forState:UIControlStateHighlighted];

                [self addFunctionItems:YES];
            }
            else {
                self.sd_width = self.initWidth;
                UIView *contentView = [self.rootViewController.view viewWithTag:kBarImageViewTag];
                [contentView removeFromSuperview];

                [weakButton setBackgroundImage:[UIImage imageNamed:@"tb3_assi_nor"] forState:UIControlStateNormal];
                [weakButton setBackgroundImage:[UIImage imageNamed:@"tb3_assi_pre"] forState:UIControlStateSelected];
                [weakButton setBackgroundImage:[UIImage imageNamed:@"tb3_assi_pre"] forState:UIControlStateHighlighted];
            }

            [UIView animateWithDuration:interval animations:^{
                self.sd_x = 0;
            }];
        }];
    }
}

- (void)addFunctionItems:(BOOL)isLeft
{
    UIView *bgView = [self.rootViewController.view viewWithTag:kBarImageViewTag];
    if (bgView) {
        [self.itemsOfBar enumerateObjectsUsingBlock:^(KodUserCenterBarFunctionItemObject *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([bgView viewWithTag:idx] == nil) {
                KodFrameTabBarItem *item = [[KodFrameTabBarItem alloc] init];
                item.tag = idx;
                item.title = obj.title;
                item.imageName = obj.imageName;
                item.sd_width = (bgView.sd_width - _itemsOfBar.count * kBarItemGap - 10) / _itemsOfBar.count;
                item.sd_height = bgView.sd_height;
                item.sd_center_y = bgView.sd_height / 2;
                item.sd_center_x = kBarItemGap + idx * (item.sd_width + kBarItemGap) + item.sd_width / 2;
                item.sd_center_x += isLeft ? 0 : item.sd_width / 4;
                [item addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];

                [bgView addSubview:item];
            }
        }];
    }
}

- (void)clickItem:(id)sender
{
    [KodNavigationView showWithIndex:0];
    [[UIApplication sharedApplication].keyWindow addSubview:[KodNavigationView instance]];
}

- (void)adjustWindowPosition
{
    if (self.sd_center_x < kScreenWidth / 2) {
        self.sd_x = 0;
    }
    else {
        self.sd_right_x = kScreenWidth;
    }
}

- (void)dragBar:(UIPanGestureRecognizer *)p
{
    CGPoint panPoint = [p locationInView:[[UIApplication sharedApplication] keyWindow]];

    if (p.state == UIGestureRecognizerStateBegan) {
        if (_button) {
            _button.selected = YES;
        }
    }
    else if(p.state == UIGestureRecognizerStateChanged) {
        self.center = CGPointMake(panPoint.x, panPoint.y);
        if (panPoint.x < WIDTH / 2) {
            self.sd_x = 0;
        }
        else if (panPoint.x > kScreenWidth - WIDTH / 2) {
            self.sd_right_x = kScreenWidth;
        }

        if (panPoint.y < HEIGHT / 2) {
            self.sd_y = 0;
        }
        else if (panPoint.y > kScreenHeight - HEIGHT / 2) {
            self.sd_bottom_y = kScreenHeight;
        }
    }
    else if(p.state == UIGestureRecognizerStateEnded) {
        if(panPoint.x <= kScreenWidth / 2) {
            [UIView animateWithDuration:0.2 animations:^{
                self.sd_x = 0;
            } completion:nil];
        }
        else {
            [UIView animateWithDuration:0.2 animations:^{
                self.sd_right_x = kScreenWidth;
            } completion:nil];
        }

        if (_button) {
            _button.selected = NO;
        }
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

Root ViewController

Root ViewController的源文件:

@interface KodBasicViewController ()

@end

@implementation KodBasicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}



- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    UIWindow *window = self.view.window;
    [window bringSubviewToFront:window.rootViewController.view];
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    UIWindow *window = self.view.window;
    [window bringSubviewToFront:window.rootViewController.view];
}

@end

2 个答案:

答案 0 :(得分:0)

这是UIWindow实例的默认背景颜色,当您的根视图正在旋转时,那些未覆盖的部分将显示为黑色块,如您所说。

您可以为窗口设置所需的背景颜色(或者在旋转时使根视图的框架足够大以覆盖整个窗口,不推荐):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  ...
  [self.window setBackgroundColor:[UIColor lightGrayColor]];
  [self.window makeKeyAndVisible];
  return YES;
}

答案 1 :(得分:0)

这是我解决这个问题的方法:

window.layer.masksToBounds = true
相关问题