仿直播頻道切換頁面的實現(xiàn)

```

#import "LivingInfoView.h"

#define Screen_width [UIScreen mainScreen].bounds.size.width

#define Screen_height [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@property (weak, nonatomic) LivingInfoView *livingInfoView;

@property (assign, nonatomic) BOOL isBeginSlid;//記錄開始觸摸的方向

@property (weak, nonatomic) UIImageView *playLayer;

@end

```


```

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor whiteColor];

//? ? 播放層

UIImageView *playLayer = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Screen_width, Screen_height)];

[self.view addSubview:playLayer];

self.playLayer = playLayer;

playLayer.image = [UIImage imageNamed:@"123.jpeg"];

playLayer.userInteractionEnabled = YES;

//? ? 播放信息層

LivingInfoView *livingInfoView = [[LivingInfoView alloc]initWithFrame:CGRectMake(0, 0, Screen_width, Screen_height)];

[playLayer addSubview:livingInfoView];

self.livingInfoView = livingInfoView;

livingInfoView.backgroundColor = [UIColor colorWithRed:242 / 255.0 green:156 / 255.0 blue:177 / 255.0 alpha:0.6];

// Do any additional setup after loading the view, typically from a nib.

}


#pragma mark - 界面的滑動

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event {

self.isBeginSlid = YES;

}


- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event {

//? ? 1.獲取手指

UITouch *touch = [touches anyObject];

//? ? 2.獲取觸摸的上一個位置

CGPoint lastPoint;

CGPoint currentPoint;

//? ? 3.獲取偏移位置

CGPoint tempCenter;

if (self.isBeginSlid) {//首次觸摸進入

lastPoint = [touch previousLocationInView:self.playLayer];

currentPoint = [touch locationInView:self.playLayer];

//判斷是左右滑動還是上下滑動

if (ABS(currentPoint.x - lastPoint.x) > ABS(currentPoint.y - lastPoint.y)) {

//? ? 3.獲取偏移位置

tempCenter = self.livingInfoView.center;

tempCenter.x += currentPoint.x - lastPoint.x;//左右滑動

//禁止向左劃

if (self.livingInfoView.frame.origin.x == 0 && currentPoint.x -lastPoint.x > 0) {//滑動開始是從0點開始的,并且是向右滑動

self.livingInfoView.center = tempCenter;

}

//? ? ? ? ? ? else if(self.livingInfoView.frame.origin.x > 0){

//? ? ? ? ? ? ? ? self.livingInfoView.center = tempCenter;

//? ? ? ? ? ? }

//? ? ? ? ? ? NSLog(@"%@-----%@",NSStringFromCGPoint(tempCenter),NSStringFromCGPoint(self.livingInfoView.center));

}else{

//? ? 3.獲取偏移位置

tempCenter = self.playLayer.center;

tempCenter.y += currentPoint.y - lastPoint.y;//上下滑動

self.playLayer.center = tempCenter;

}

}else{//滑動開始后進入,滑動方向要么水平要么垂直

if (self.playLayer.frame.origin.y != 0){//垂直的優(yōu)先級高于左右滑,因為左右滑的判定是不等于0

lastPoint = [touch previousLocationInView:self.playLayer];

currentPoint = [touch locationInView:self.playLayer];

tempCenter = self.playLayer.center;

tempCenter.y += currentPoint.y -lastPoint.y;

self.playLayer.center = tempCenter;

}else if (self.livingInfoView.frame.origin.x != 0) {

lastPoint = [touch previousLocationInView:self.livingInfoView];

currentPoint = [touch locationInView:self.livingInfoView];

tempCenter = self.livingInfoView.center;

tempCenter.x += currentPoint.x -lastPoint.x;

//禁止向左劃

if (self.livingInfoView.frame.origin.x == 0 && currentPoint.x -lastPoint.x > 0) {//滑動開始是從0點開始的,并且是向右滑動

self.livingInfoView.center = tempCenter;

}else if(self.livingInfoView.frame.origin.x > 0){

self.livingInfoView.center = tempCenter;

}

}

}

self.isBeginSlid = NO;

}


- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event {

//? ? NSLog(@"%.2f-----%.2f",livingInfoView.frame.origin.y,Screen_height * 0.8);

//? ? 水平滑動判斷

//在控制器這邊滑動判斷如果滑動范圍沒有超過屏幕的十分之八livingInfoView還是離開屏幕

if (self.livingInfoView.frame.origin.x > Screen_width * 0.8) {

[UIView animateWithDuration:0.06 animations:^{

CGRect frame = self.livingInfoView.frame;

frame.origin.x = Screen_width;

self.livingInfoView.frame = frame;

}];

}else{//否則則回到屏幕0點

[UIView animateWithDuration:0.2 animations:^{

CGRect frame = self.livingInfoView.frame;

frame.origin.x = 0;

self.livingInfoView.frame = frame;

}];

}

//? ? 上下滑動判斷

if (self.playLayer.frame.origin.y > Screen_height * 0.2) {

//? ? ? ? 切換到下一頻道

self.playLayer.image = [UIImage imageNamed:@"PlayView2"];

}else if (self.playLayer.frame.origin.y < - Screen_height * 0.2){

//? ? ? ? 切換到上一頻道

self.playLayer.image = [UIImage imageNamed:@"PlayView"];

}

//? ? ? ? 回到原始位置等待界面重新加載

CGRect frame = self.playLayer.frame;

frame.origin.y = 0;

self.playLayer.frame = frame;

}


- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent *)event{

//? ? 水平滑動判斷

//在控制器這邊滑動判斷如果滑動范圍沒有超過屏幕的十分之八livingInfoView還是離開屏幕

if (self.livingInfoView.frame.origin.x > Screen_width * 0.8) {

[UIView animateWithDuration:0.06 animations:^{

CGRect frame = self.livingInfoView.frame;

frame.origin.x = Screen_width;

self.livingInfoView.frame = frame;

}];

}else{//否則則回到屏幕0點

[UIView animateWithDuration:0.2 animations:^{

CGRect frame = self.livingInfoView.frame;

frame.origin.x = 0;

self.livingInfoView.frame = frame;

}];

}

//? ? 上下滑動判斷

if (self.livingInfoView.frame.origin.y > Screen_height * 0.2) {

//? ? ? ? 切換到下一頻道,重新加載界面,這里用切換圖片做演示。

self.livingInfoView.backgroundColor

= [UIColor colorWithRed:arc4random_uniform(256) / 255.0

green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) /

255.0 alpha:0.5];

}else if (self.livingInfoView.frame.origin.y < - Screen_height * 0.2){

//? ? ? ? 切換到上一頻道,重新加載界面,這里用切換圖片做演示。

self.livingInfoView.backgroundColor

= [UIColor colorWithRed:arc4random_uniform(256) / 255.0

green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) /

255.0 alpha:0.5];

}

//? ? ? ? 回到原始位置等待界面重新加載

CGRect frame = self.livingInfoView.frame;

frame.origin.y = 0;

self.livingInfoView.frame = frame;

}

```

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • VLC的集成和使用 VLC介紹 VLC Media Player (VideoLAN) 為 Windows、Lin...
    Pocket閱讀 20,385評論 75 66
  • 1、禁止手機睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa閱讀 1,212評論 1 6
  • 一:實現(xiàn)效果 二:實現(xiàn)思路 1:配置參數(shù)添加到ViewController - (void)viewDidLoad...
    歐大帥Allen閱讀 1,870評論 0 4
  • 百解紅塵苦畫心, 鶯前柳唱水邊吟。 暫擱柔筆銜眉望, 滿紙春風為誰顰?
    不如_9f46閱讀 227評論 0 1
  • 我發(fā)現(xiàn)一個問題!我最近太癡迷于看手機,看微信,看理財收益及相關文章!最近的心思一直是飄忽不定的,想旅行,想有份事業(yè)...
    璽媽閱讀 356評論 0 0

友情鏈接更多精彩內容