|
//
// TPWindow.m
// ThePaperHD
//
// Created by scar1900 on 15/2/5.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "TPWindow.h"
#import "AppDelegate.h"
#import "YRSideViewController.h"
#import "TPMovieContent.h"
#import "TPLiveHomeController.h"
@interface TPWindow()<UIAlertViewDelegate,UIGestureRecognizerDelegate> {
BOOL longGesFlag;
CGRect embeddedVideoRect;
}
@property(nonatomic, weak)UIAlertView *alertView;
@property(nonatomic, weak)UIViewController *player;
@end
@implementation TPWindow
- (id) initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self addEmbeddedVideoGesture];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(linkWithPush:) name:LINKWITHPUSH object:nil];
}
return self;
}
- (UIView*)nightModeMaskView {
if (!_nightModeMaskView) {
_nightModeMaskView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
_nightModeMaskView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
_nightModeMaskView.userInteractionEnabled = NO;
_nightModeMaskView.autoresizingMask = AutoresizingFull;
_nightModeMaskView.hidden = YES;
}
return _nightModeMaskView;
}
- (void)setMaskAfterOpenNightMode {
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
[self setNoNightModeMask];
self.nightModeMaskView.hidden = NO;
self.nightModeMaskView.frame = [UIScreen mainScreen].bounds;
[self addSubview:self.nightModeMaskView];
}else {
self.nightModeMaskView.hidden = YES;
[self setNoNightModeMask];
}
}
- (void)setNoNightModeMask {
[self.nightModeMaskView removeFromSuperview];
_nightModeMaskView = nil;
}
- (void)linkWithPush:(NSNotification*)notification {
TPLOG(@"接到通知或者广告点击:%@",[TPUserDefault instance].pushBO);
AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
YRSideViewController *sideController=[delegate sideViewController];
if ([[TPUserDefault instance].pushBO.isComeFromRemote intValue] != 1) {
if (isPad) {
presentAfterPushOrClickAd();
}else {
pushToContentAfterPushOrClickAd(sideController.navigationController);
}
}else {
if ([[TPUserDefault instance].isEnterFromBackground intValue] == 1) {
NSDictionary *dic = [TPUserDefault instance].pushBO.aps;
if (_alertView) {
[_alertView dismissWithClickedButtonIndex:_alertView.cancelButtonIndex animated:NO];
_alertView = nil;
}
UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:@"澎湃新闻" message:dic[@"alert"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"查看",nil];
alertView.delegate = self;
[alertView show];
/**
* bug:6002(推送:app运行中,连着收到两条推送,点击第二条推送,第一条推送的对话框依然存在)
*/
_alertView = alertView;
}else {
if (KEY_WINDOW.rootViewController.presentedViewController) {
[KEY_WINDOW.rootViewController dismissViewControllerAnimated:YES completion:^{
if (isPad) {
presentAfterPushOrClickAd();
}else {
pushToContentAfterPushOrClickAd(sideController.navigationController);
}
[TPUserDefault instance].pushBO = nil;
}];
}else {
if (isPad) {
presentAfterPushOrClickAd();
}else {
pushToContentAfterPushOrClickAd(sideController.navigationController);
}
[TPUserDefault instance].pushBO = nil;
}
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
YRSideViewController *sideController=[delegate sideViewController];
if (buttonIndex != 0) {
/**
* bug:6107(倒退】视频横屏播放,点击推送查看,页面异常)
*/
UIViewController *vc = ((UINavigationController*)self.rootViewController).viewControllers.lastObject;
if ([vc isKindOfClass:[TPLiveHomeController class]]){
[(TPLiveHomeController *)vc setIsClose:YES];
}
[[UIApplication sharedApplication]
setStatusBarOrientation:UIInterfaceOrientationPortrait
animated:NO];
[[UIDevice currentDevice]
setValue:[NSNumber
numberWithInteger:UIInterfaceOrientationPortrait]
forKey:@"orientation"];
[[NSNotificationCenter defaultCenter] postNotificationName:PUSHCLICK object:nil userInfo:nil];
if (isPad) {
if (KEY_WINDOW.rootViewController.presentedViewController) {
[KEY_WINDOW.rootViewController dismissViewControllerAnimated:YES completion:^{
presentAfterPushOrClickAd();
[TPUserDefault instance].pushBO = nil;
}];
}else {
presentAfterPushOrClickAd();
[TPUserDefault instance].pushBO = nil;
}
}else {
pushToContentAfterPushOrClickAd(sideController.navigationController);
[TPUserDefault instance].pushBO = nil;
}
}
}
-(void) willPresentAlertView:(UIAlertView *)alertView
{
for( UIView * view in alertView.subviews )
{
if( [view isKindOfClass:[UILabel class]] )
{
UILabel* label = (UILabel*) view;
label.textAlignment=NSTextAlignmentLeft;
}
}
}
- (void)addEmbeddedVideoGesture {
self.embeddedVideoGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(embeddedVideoHandler:)];
self.embeddedVideoGesture.enabled = NO;
longGesFlag = NO;
self.embeddedVideoGesture.delegate = self;
[self addGestureRecognizer:self.embeddedVideoGesture];
}
- (void)enabelEmbeddedVideoGesture:(BOOL)enable
withVideoController:(UIViewController*)movieContent
embeddedRect:(CGRect)rect{
self.embeddedVideoGesture.enabled = enable;
self.player = movieContent;
embeddedVideoRect = rect;
}
- (void)embeddedVideoHandler:(UIPanGestureRecognizer*)gesture {
if (!self.player) {
return;
}
UIGestureRecognizerState state = gesture.state;
CGPoint location = [gesture locationInView:self.player.view];
CGPoint center = [gesture locationInView:self];
// TPLOG(@"%f=========%f",location.x,location.y);
if (state == UIGestureRecognizerStateBegan) {
if (location.x < 0
|| location.y<0
|| location.x>self.player.view.bounds.size.width
|| location.y>self.player.view.bounds.size.height) {
return;
}
longGesFlag = YES;
}else if (state == UIGestureRecognizerStateChanged) {
if (!longGesFlag) {
return;
}
TPMovieContent *movieContent = (TPMovieContent*)self.player;
embeddedVideoRect.origin.x = center.x - embeddedVideoRect.size.width/2;
embeddedVideoRect.origin.y = center.y - embeddedVideoRect.size.height/2;
if (embeddedVideoRect.origin.x < 0) {
embeddedVideoRect.origin.x = 0;
}
if (embeddedVideoRect.origin.x + embeddedVideoRect.size.width > self.bounds.size.width) {
embeddedVideoRect.origin.x = self.bounds.size.width - embeddedVideoRect.size.width;
}
if (embeddedVideoRect.origin.y < 0) {
embeddedVideoRect.origin.y = 0;
}
if (embeddedVideoRect.origin.y + embeddedVideoRect.size.height > self.bounds.size.height) {
embeddedVideoRect.origin.y = self.bounds.size.height - embeddedVideoRect.size.height;
}
[movieContent relayoutSubViewForScreen:embeddedVideoRect fullScreen:NO];
}else {
longGesFlag = NO;
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
- (void)closeAndSotoEmbeddedVideo {
TPMovieContent *movieContent = (TPMovieContent*)self.player;
if (movieContent) {
[movieContent.view removeFromSuperview];
[movieContent selfDealloc];
movieContent = nil;
movieContent.view = nil;
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
|