ipad 视频播放器的渐变播放效果

本文介绍了一种在iOS应用中实现视频播放时渐变过渡效果的方法。通过自定义UIView子类并利用UIView动画,实现了视频播放时的渐变进入和退出效果。此外,还介绍了如何设置双击手势以触发特定事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现视频播放的渐变切换效果,可以修改代码在iphone上使用。

//
// FLWPlayer.h
//
// Created by scott.8an@gmail.com on 11-10-28.
// Copyright 2011 littleWorn inc. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@class FLWPlayer;

@protocol FLWPlayerDelegate <NSObject>
-(void)singleTapView:(FLWPlayer*)view;
@end

//interface
@interface FLWPlayer : UIView{
@private
id<FLWPlayerDelegate>_delegate;
MPMoviePlayerController *_player;
NSURL *_url;
}
@property(assign) id<FLWPlayerDelegate> delegate;
@property(nonatomic,retain) NSURL *url;
@property(nonatomic,retain) MPMoviePlayerController *player;

-(id)initWithURL:(NSURL*)url;
-(id)initWithFrame:(CGRect)frame url:(NSURL*)url;

-(void)play;
-(void)stop;
-(void)pause;
@end



//
// FLWPlayer.m
//
// Created by scott.8an@gmail.com on 11-10-28.
// Copyright 2011 littleWorn inc. All rights reserved.
//
#import "FLWPlayer.h"

@implementation FLWPlayer
@synthesize delegate = _delegate;
@synthesize url = _url;
@synthesize player = _player;

#pragma mark life cycle
- (void)dealloc {
[_delegate release];
[_url release];
[_player release];
[super dealloc];
}

//init
-(id)init{
if (self = [super init]) {
//去掉状态栏
[[UIApplication sharedApplication] setStatusBarHidden:YES];

UITapGestureRecognizer *tg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
tg.numberOfTapsRequired = 2;
[self addGestureRecognizer:tg];
[tg release];
}
return self;
}

-(id)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
//去掉状态栏
[[UIApplication sharedApplication] setStatusBarHidden:YES];

UITapGestureRecognizer *tg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
tg.numberOfTapsRequired = 2;
[self addGestureRecognizer:tg];
[tg release];
}
return self;
}

-(id)initWithURL:(NSURL*)url{
if (self = [super init]) {
self.url = url;

//去掉状态栏
[[UIApplication sharedApplication] setStatusBarHidden:YES];

UITapGestureRecognizer *tg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
tg.numberOfTapsRequired = 2;
[self addGestureRecognizer:tg];
[tg release];
}
return self;
}

-(id)initWithFrame:(CGRect)frame url:(NSURL*)url{
if (self = [super initWithFrame:frame]) {
self.url = url;

//去掉状态栏
[[UIApplication sharedApplication] setStatusBarHidden:YES];

UITapGestureRecognizer *tg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
tg.numberOfTapsRequired = 2;
[self addGestureRecognizer:tg];
[tg release];
}
return self;
}

-(void)singleTap{
[self.delegate singleTapView:self];
}

#pragma mark overide
//-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// [self.delegate singleTapView:self];
//}


//播放视频
-(void)play{
//覆盖
UIView *cover = [[UIView alloc] initWithFrame:CGRectMake(0,0, 1024, 768)];
cover.tag = 100;
cover.backgroundColor = [UIColor blackColor];
cover.alpha = 0.0f;
//添加覆盖视图实现渐变退出效果
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.4f];
[UIView setAnimationDelay:0.2f];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(stopPlaying)];
cover.alpha = 1.0f;
[UIView commitAnimations];
[self addSubview:cover];
[cover release];
self.backgroundColor = [UIColor blackColor];
}
//开始播放
-(void)startToPlay{
if (nil == self.url) {
return;
}

//清除覆盖
UIView *cover = [self viewWithTag:100];
if (cover) {
[cover removeFromSuperview];
}
//加载视频
if (_player) {
[_player setContentURL:self.url];
}else {
self.player = [[MPMoviePlayerController alloc] initWithContentURL:self.url];
_player.view.frame = CGRectMake(0, 0, 1024, 768);
_player.controlStyle= MPMovieControlStyleNone;
_player.repeatMode = MPMovieRepeatModeOne;
[_player setFullscreen:YES];
[self addSubview:_player.view];
[_player release];
}
//开始播放
[_player prepareToPlay];
[_player play];

//渐变进入
_player.view.alpha = 0.0f;
_player.backgroundView.backgroundColor = [UIColor blackColor];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
_player.view.alpha = 1.0f;
[UIView commitAnimations];
}

//停止正在播放的视频
-(void)stopPlaying{
[self stop];
[self startToPlay];
}

//停止播放
-(void)stop{
if (_player) {
[_player stop];
}
}

//暂停播放
-(void)pause{
if (_player) {
[_player pause];
}
}
@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值