1:控制器界面进行提前加载View;
-
(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_isLocked) {
return;
}
UITouch *touch = [touches anyObject];
CGPoint currentLocation = [touch locationInView:self.playerController.view];
CGPoint prevLocation = [touch previousLocationInView:self.playerController.view];
CGFloat offset_x = currentLocation.x - _originalLocation.x;
CGFloat offset_y = currentLocation.y - _originalLocation.y;if (CGPointEqualToPoint(_originalLocation,CGPointZero)) {
_originalLocation = currentLocation;
return;
}
_originalLocation = currentLocation;
CGRect frame = [UIScreen mainScreen].bounds;
if ((currentLocation.x > frame.size.height*0.5) && (ABS(offset_x) < ABS(offset_y))) {
pragma mark --- 判断是点击还是滑动
if (currentLocation.y - prevLocation.y != 0) {
_gestureType = GestureTypeOfVolume;
} else {
_gestureType = GestureTypeOfNone;
}
}
else if ((currentLocation.x < frame.size.height*0.5) && (ABS(offset_x) <= ABS(offset_y)))
{
if (currentLocation.y - prevLocation.y != 0) {
_gestureType = GestureTypeOfBrightness;
} else {
_gestureType = GestureTypeOfNone;
}
}else if ((ABS(offset_x) > ABS(offset_y)))
{
if (currentLocation.x - prevLocation.x != 0) {
_gestureType = GestureTypeOfProgress;
// NSLog(@"进度");
} else {
_gestureType = GestureTypeOfNone;
//finger touch went downwards
}
}
if (_gestureType == GestureTypeOfProgress)
{
if (ABS(offset_x)<1.0) {
return;
}
self.mViewProgress.hidden = NO;
if (offset_x < -2.0) {
[self progressAdd:-progressStep];
self.KProgressimage.image = [UIImage imageNamed:@"kuaitui"];
}
if (offset_x > 2.0) {
[self progressAdd:progressStep];
self.KProgressimage.image = [UIImage imageNamed:@"kuaijin"];
}
}
else if (_gestureType == GestureTypeOfVolume)
{
NSLog(@"上下滑动的距离 %f",offset_y);
if (offset_y > 2.0)
{
[self volumeAdd:-VolumeStep];
}else if (offset_y < -2.0)
{
[self volumeAdd:VolumeStep];
}
else{
}
}else if (_gestureType == GestureTypeOfBrightness)
{
if (offset_y > 0)
{
[self brightnessAdd:-VolumeStep];
}else{
[self brightnessAdd:VolumeStep];
}
}
}
//音量调节
- (void)volumeAdd:(CGFloat)step {
[MPMusicPlayerController applicationMusicPlayer].volume += step;
}
//亮度调节 - (void)brightnessAdd:(CGFloat)step
{
CGFloat currentLight = [[UIScreen mainScreen] brightness];
currentLight += step;
[[UIScreen mainScreen] setBrightness:currentLight]; //重点啊。
}
//下面是View展示UI的代码
//使用别人创建好的类,这个View创建的还可以,最好优化下就OK了
@interface BrightnessView ()
@property (nonatomic, strong) UIImageView *backImage;
@property (nonatomic, strong) UILabel *title;
@property (nonatomic, strong) UIView *brightnessLevelView;
@property (nonatomic, strong) NSMutableArray *tipArray;
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation BrightnessView
pragma mark - 懒加载
-(UILabel *)title {
if (!_title) {
_title = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, self.bounds.size.width, 30)];
_title.font = [UIFont boldSystemFontOfSize:16];
_title.textColor = [UIColor colorWithRed:0.25f green:0.22f blue:0.21f alpha:1.00f];
_title.textAlignment = NSTextAlignmentCenter;
_title.text = @"亮度调节";
}
return _title;
}
-
(UIImageView *)backImage {
if (!_backImage) {
_backImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 79, 76)];
_backImage.image = [UIImage imageNamed:@"brightness"];
}
return _backImage;
}
-(UIView *)brightnessLevelView {
if (!_brightnessLevelView) {
_brightnessLevelView = [[UIView alloc]initWithFrame:CGRectMake(13, 132, self.bounds.size.width - 26, 7)];
_brightnessLevelView.backgroundColor = [UIColor colorWithRed:0.25f green:0.22f blue:0.21f alpha:1.00f];
[self addSubview:_brightnessLevelView];
}
return _brightnessLevelView;
}
pragma mark - 单例
- (instancetype)sharedBrightnessView {
static BrightnessView *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[BrightnessView alloc] init];
[[[UIApplication sharedApplication].windows firstObject] addSubview:instance];
});
return instance;
}
(instancetype)init {
if (self = [super init]) {
[self setupUI];
}
return self;
}(void)setupUI {
self.frame = CGRectMake(ZLScreenWidth * 0.5, ZLScreenHeight * 0.5 - 20, 155, 155);
self.layer.cornerRadius = 10;
self.layer.masksToBounds = YES;
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:self.bounds];
[self addSubview:toolbar];
[self addSubview:self.backImage];
[self addSubview:self.title];
[self addSubview:self.brightnessLevelView];
[self createTips];
[self addStatusBarNotification];
[self addKVOObserver];
self.alpha = 0.0;
}
pragma makr - Tips的创建
-
(void)createTips {
self.tipArray = [NSMutableArray arrayWithCapacity:16];
CGFloat tipW = (self.brightnessLevelView.bounds.size.width - 17) / 16;
CGFloat tipH = 5;
CGFloat tipY = 1;for (int i = 0; i < 16; i++) {
CGFloat tipX = i * (tipW + 1) + 1;
UIImageView *image = [[UIImageView alloc] init];
image.backgroundColor = [UIColor whiteColor];
image.frame = CGRectMake(tipX, tipY, tipW, tipH);
[self.brightnessLevelView addSubview:image];
[self.tipArray addObject:image];
}
[self updateBrightnessLevel:[UIScreen mainScreen].brightness];
} (void)addStatusBarNotification {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(statusBarOrientationNotification:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
}(void)addKVOObserver {
[[UIScreen mainScreen] addObserver:self
forKeyPath:@"brightness"
options:NSKeyValueObservingOptionNew context:NULL];
}-
(void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {CGFloat levelValue = [change[@"new"] floatValue];
[self removeTimer];
[self appearBrightnessView];
[self updateBrightnessLevel:levelValue];
}
pragma mark - 状态栏方向改变当前View的方向,UI更新
- (void)statusBarOrientationNotification:(NSNotification *)notify {
[self setNeedsLayout];
}
pragma mark - Brightness显示
- (void)appearBrightnessView {
[UIView animateWithDuration:0.5 animations:^{
self.alpha = 1;
} completion:^(BOOL finished) {
[self addtimer];
}];
}
//隐藏 - (void)disAppearBrightnessView {
if (self.alpha == 1.0) {
[UIView animateWithDuration:0.5 animations:^{
self.alpha = 0.0;
} completion:^(BOOL finished) {
[self removeTimer];
}];
}
}
pragma mark -关闭时间
(void)addtimer {
if (self.timer) {
return;
}
self.timer = [NSTimer timerWithTimeInterval:1
target:self
selector:@selector(disAppearBrightnessView)
userInfo:nil
repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}(void)removeTimer{
[self.timer invalidate];
self.timer = nil;
}
pragma mark - 亮度值展示
- (void)updateBrightnessLevel:(CGFloat)brightnessLevel {
CGFloat stage = 1 / 15.0;
NSInteger level = brightnessLevel / stage;
for (int i = 0; i < self.tipArray.count; i++) {
UIImageView *img = self.tipArray[i];
if (i <= level) {
img.hidden = NO;
} else {
img.hidden = YES;
}
}
}
pragma mark - 刷新VIew
-
(void)layoutSubviews {
[super layoutSubviews];
UIInterfaceOrientation currInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
switch (currInterfaceOrientation)
{
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationPortraitUpsideDown:
{
self.center = CGPointMake(ZLScreenWidth * 0.5, (ZLScreenHeight - 10) * 0.5);
}
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
{
self.center = CGPointMake(ZLScreenWidth * 0.5, (ZLScreenHeight) * 0.5);
}
break;
default:
break;
}
self.backImage.center = CGPointMake(155 * 0.5, 155 * 0.5);
[self.superview bringSubviewToFront:self];
}
@end