我通过在互联网上找到的片段嵌入了 YouTube 的视频,以下是我使用的代码:
@interface FirstViewController (Private)
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self embedYouTube:@"http://www.youtube.com/watch?v=l3Iwh5hqbyE" frame:CGRectMake(20, 20, 100, 100)];
}
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
NSString *embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:html baseURL:nil];
[self.view addSubview:videoView];
[videoView release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
它编译正确,当我打开它时,我看到代码创建的一个白色框位置不正确。我对此有两个问题:
我如何知道视频是否会播放,它只是一个普通的白框,模拟器是否播放 YouTube 上的视频?
我该如何定位这个盒子?
刚刚测试了您的代码,它在 iPhone 上运行良好,但 iOS 模拟器不支持 Youtube 视频,因此您需要一个真实的设备进行测试。
我该如何定位这个盒子?
您已经在这一行传递了盒子的 X (20)、Y(20)、宽度(100) 和高度(100):
[self embedYouTube:@"http://..." frame:CGRectMake(20, 20, 100, 100)];
要随后更改视图的位置,请修改其 center 属性:
videoView.center = CGPointMake(200, 100 );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)