-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.m
158 lines (138 loc) · 5.6 KB
/
ViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// ViewController.m
// Demo_VideoPlay
//
// Created by ule_zhangfanglin on 2017/1/23.
// Copyright © 2017年 admin. All rights reserved.
//
#import "ViewController.h"
//设备屏幕大小
#define __MainScreenFrame [[UIScreen mainScreen] bounds]
//设备屏幕宽
#define __MainScreen_Width __MainScreenFrame.size.width
UIView * play_view;
UIButton * playButton;
BOOL hidden;
#define __MainScreen_Height __MainScreenFrame.size.height
@interface ViewController ()
@end
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
UITableView * m_tableView=[[UITableView alloc] initWithFrame:CGRectMake(20, 20, __MainScreen_Width-40, __MainScreen_Height)];
m_tableView.delegate=self;
m_tableView.dataSource=self;
[self.view addSubview:m_tableView];
[self.view setBackgroundColor:[UIColor whiteColor]];
play_view=[[UIView alloc] initWithFrame:CGRectMake(30,25 , __MainScreen_Width-40, 140)];
[play_view setBackgroundColor:[UIColor redColor]];
playButton =[[UIButton alloc] initWithFrame:CGRectMake(40,40 ,50 ,50 )];
[playButton setTitle:@"放大" forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[playButton setBackgroundColor:[UIColor brownColor]];
[play_view addSubview:playButton];
[playButton setUserInteractionEnabled:YES];
[self.view addSubview:play_view];
hidden=NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)buttonAction:(id)sender
{
if ([playButton.titleLabel.text isEqualToString:@"放大"]) {
NSNumber *orientationLeft = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:orientationLeft forKey:@"orientation"];
[playButton setTitle:@"缩小" forState:UIControlStateNormal];
hidden=YES;
}
else
{
NSNumber *orientationPortrait = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:orientationPortrait forKey:@"orientation"];
[playButton setTitle:@"放大" forState:UIControlStateNormal];
hidden=NO;
}
[self setNeedsStatusBarAppearanceUpdate];
// [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeRight]; ios9.0
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 150;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * tableViewCell= [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"];
UIView * view;
if (!tableViewCell) {
tableViewCell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableViewCell"];
[tableViewCell setSelectionStyle:UITableViewCellSelectionStyleNone];
view =[[UIView alloc] initWithFrame:CGRectMake(10, 5,__MainScreen_Width-60 , 140)];
[view setBackgroundColor:[UIColor grayColor]];
[tableViewCell addSubview:view];
}
return tableViewCell;
}
-(BOOL)prefersStatusBarHidden
{
if (hidden) {
return YES;
}
else
{
return NO;
}
}
-(BOOL)shouldAutorotate
{
return NO;
}
//横屏方法
- (void)orientChangeWithVideoSuperView:(UIView *)superView playerView:(UIView *)view playerViewRect:(CGRect)rect{
UIDeviceOrientation orient = [UIDevice currentDevice].orientation;
play_view.backgroundColor = [UIColor redColor];
superView.layer.transform = CATransform3DIdentity;
switch (orient)
{
case UIDeviceOrientationPortrait:
case UIDeviceOrientationPortraitUpsideDown:
case UIDeviceOrientationFaceUp:
case UIDeviceOrientationFaceDown:
case UIDeviceOrientationUnknown:
superView.frame = [UIScreen mainScreen].bounds;
view.frame = rect;
[playButton setTitle:@"放大" forState:UIControlStateNormal];
break;
case UIDeviceOrientationLandscapeLeft:
case UIDeviceOrientationLandscapeRight:
superView.frame = CGRectMake(-CGRectGetHeight([UIScreen mainScreen].bounds)+CGRectGetWidth([UIScreen mainScreen].bounds), 0, CGRectGetHeight([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds));
view.frame = CGRectMake(0, 0,CGRectGetHeight([UIScreen mainScreen].bounds),CGRectGetWidth([UIScreen mainScreen].bounds));
superView.transform = CGAffineTransformMakeRotation(M_PI_2);
[playButton setTitle:@"缩小" forState:UIControlStateNormal];
break;
default:
break;
}
}
//通知设备旋转了
- (void)orientChange:(NSNotification *)noti
{
[self orientChangeWithVideoSuperView:self.view playerView:play_view playerViewRect:CGRectMake(30,25 , __MainScreen_Width-40, 140)];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end