@interface DetailsViewController ()<UIGestureRecognizerDelegate,UIActionSheetDelegate,UIWebViewDelegate> {
NSString *strUrl; //图片网址
int lastContentOffset; //最后位置
NSString *strSize;
int labelSize ;
int tempY; //设置视图的y轴
DetailButtonView *detailButtonView;
}
@property (strong, nonatomic) UIWebView *webView;
@property (strong, nonatomic) LoadreImageView *imgView;//保存网络图片到本地相册
@property (strong, nonatomic) UIView *whiteBackground; //白色覆盖背景
@property (strong, nonatomic) UIView *viewSetting; //设置视图
@property (strong, nonatomic) UIButton *btnGrayView; //灰色覆盖层
@property (strong, nonatomic) UISegmentedControl *sengment; //字体选择器;
@end
static BOOL openImage = YES; //判断是否打开图片状态
@implementation DetailsViewController
- (void)viewWillAppear:(BOOL)animated {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
labelSize = (int)[defaults integerForKey:@"selectionRow"];
NSString *str = [self laelSize:labelSize];
self.tabBarController.tabBar.hidden = NO;
self.navigationController.navigationBarHidden = YES;
strSize = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%@'",str];
self.sengment.selectedSegmentIndex = labelSize;
}
- (void)viewDidLoad {
self.imgView = [LoadreImageView new];
self.whiteBackground = [[UIView alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT - 20)];
self.whiteBackground.backgroundColor = [UIColor whiteColor];
self.whiteBackground.hidden =YES;
[self.view addSubview:self.whiteBackground];
//状态栏目背景颜色
UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
statusBarView.backgroundColor=[UIColor blackColor];
[self.view addSubview:statusBarView];
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
// Do any additional setup after loading the view.
//创建webView
self.webView = [UIWebView new];
self.webView.frame = CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 60);
self.webView.delegate = self;
self.webView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.webView];
//读取html文件
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"1" ofType:@"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:filePath]];
//,弹出界面设置view 的时候 显示灰色的view,点击时,回收设置view
self.btnGrayView = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.btnGrayView.frame = self.view.bounds;
self.btnGrayView.backgroundColor = [UIColor grayColor];
self.btnGrayView.alpha = 0.3;
[self.btnGrayView addTarget:self action:@selector(btnGrayViewAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.btnGrayView];
self.btnGrayView.hidden = YES;
//设置视图
[self settingView];
//创建底部状态栏
detailButtonView = [[DetailButtonView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 40, self.view.frame.size.width, 40)]; //160;
[detailButtonView.btnMore addTarget:self action:@selector(btnMoreAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:detailButtonView];
//NSString *fontFamily = @"STHeiti";//改字体
NSString *fontFamily = FONT_NAME2;
float fontSize = 16;
UIColor *fontColor = [UIColor blackColor];
NSString *jsString = [NSString stringWithFormat:@"<html> \n"
"<head> \n"
"<style type=\"text/css\"> \n"
"body {font-size: %f; font-family: \"%@\"; color: %@;}\n"
"</style> \n"
"</head> \n"
"<body>%@</body> \n"
"</html>", fontSize, fontFamily, fontColor, htmlString];
[self.webView loadHTMLString:jsString baseURL:nil];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
//doubleTap.numberOfTouchesRequired = 2;
doubleTap.numberOfTapsRequired = 1;
doubleTap.delegate = self;
doubleTap.cancelsTouchesInView = NO;
[self.webView addGestureRecognizer:doubleTap];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longGesture:)];
[self.view addGestureRecognizer:longPress];
//活动指示器 开始
//[[LoadingProgressView getInstance] show];
}
- (void)btnGrayViewAction:(UIButton *)sender {
[UIView animateWithDuration:0.5 animations:^{
self.viewSetting.center = CGPointMake(SCREEN_WIDTH/2,(SCREEN_HEIGHT - 40) + 60);
}];
self.btnGrayView.hidden = YES;
}
- (void)btnMoreAction:(UIButton *)sender {
if (tempY == SCREEN_HEIGHT- 40) {
tempY = SCREEN_HEIGHT - 160;
self.btnGrayView.hidden = NO;
[UIView animateWithDuration:0.3 animations:^{
detailButtonView.btnMore.transform = CGAffineTransformMakeRotation(M_PI);
}];
} else {
tempY = SCREEN_HEIGHT - 40;
self.btnGrayView.hidden = YES;
[UIView animateWithDuration:0.3 animations:^{
detailButtonView.btnMore.transform = CGAffineTransformMakeRotation(0);
}];
}
[UIView animateWithDuration:0.5 animations:^{
self.viewSetting.center = CGPointMake(SCREEN_WIDTH/2,tempY + 60);
}];
}
- (void)segmentAction:(UISegmentedControl *)seg {
int index = (int)seg.selectedSegmentIndex;
NSString *str = [self laelSize:index];
NSString *strSize1 = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%@'",str];
[self.webView stringByEvaluatingJavaScriptFromString:strSize1];
//保存选中的行数
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger: index forKey:@"selectionRow"];
[defaults synchronize];
}
- (NSString *)laelSize:(int)index {
NSString *str;
switch (index) {
case 0:
str = @"100%";
break;
case 1:
str = @"125%";
break;
case 2:
str = @"150%";
break;
case 3:
str = @"175%";
break;
default:
break;
}
return str;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.webView stringByEvaluatingJavaScriptFromString:strSize];
//[webView stringByEvaluatingJavaScriptFromString:strxx];
//NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
//NSLog(@"curretURL=%@=",currentURL);
//NSString *jsString = [[NSString alloc] initWithFormat:@"document.body.style.fontSize=%f;document.body.style.color=%@",25,fontColor];
//活动指示器 隐藏
//[[LoadingProgressView getInstance] hidde];
//拦截网页图片 并修改图片大小
int imgWidth = self.view.frame.size.width - 30;
NSString *strImage = [NSString stringWithFormat:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function ResizeImages() { "
"var myimg,oldwidth;"
"var maxwidth=%d;"
"for(i=0;i <document.images.length;i++){"
"myimg = document.images[i];"
"if(myimg.width > maxwidth){"
"oldwidth = myimg.width;"
"myimg.width = maxwidth;"
"myimg.height = myimg.height * (maxwidth/oldwidth);"
"}"
"}"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);",imgWidth];
[webView stringByEvaluatingJavaScriptFromString:
strImage];
[webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
/*/字体颜色
NSString *fontColor = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '%@'",strFontColor];
[webView stringByEvaluatingJavaScriptFromString:fontColor];
//页面背景色 #2E2E2E
NSString *viewBackground = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.background='%@'",strBackground];
[webView stringByEvaluatingJavaScriptFromString:viewBackground];*/
//NSLog(@"model====XXX=%@=%@=",strFontColor,strBackground);
//NSString *fontFamily = @"STHeiti"; Helvetica-Bold Arial @"STHeiti"
//NSString *fontFamily = FONT_NAME2;
//字体
// NSString *str =[NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.WebkitFontFamily='Helvetica-Bold'"];//font-family
// [webView stringByEvaluatingJavaScriptFromString:str];
}
//点击获取图片
-(void)doubleTap:(UITapGestureRecognizer *)sender
{
if (openImage == YES) {
CGPoint pt = [sender locationInView:self.webView];
pt.x = SCREEN_WIDTH/2;
NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", pt.x, pt.y];
NSString *urlToSave = [self.webView stringByEvaluatingJavaScriptFromString:imgURL];
// NSLog(@"image url=%@", urlToSave);
strUrl = urlToSave;
if (urlToSave.length > 0) {
[self showImageURL:urlToSave point:pt];
[self.imgView loadImage:urlToSave];
}
}
}
//呈现图片
-(void)showImageURL:(NSString *)url point:(CGPoint)point {
LoadreImageView *showView = [[LoadreImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 270)];
showView.center = point;
[UIView animateWithDuration:0.5f animations:^{
CGPoint newPoint = self.view.center;
//newPoint.y += 10;
showView.center = newPoint;
}];
showView.backgroundColor = [UIColor blackColor];
showView.userInteractionEnabled = YES;
[showView loadImage:strUrl];
self.whiteBackground.hidden = NO;
self.whiteBackground.userInteractionEnabled = NO;
[self.view addSubview:self.whiteBackground];
[self.view addSubview:showView];
openImage = NO;
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleViewTap:)];
[showView addGestureRecognizer:singleTap];
//[self.navigationController setNavigationBarHidden:YES animated:YES];
}
//移除图片查看视图
-(void)handleSingleViewTap:(UITapGestureRecognizer *)sender
{
for (id obj in self.view.subviews) {
if ([obj isKindOfClass:[LoadreImageView class]]) {
[obj removeFromSuperview];
}
}
openImage = YES;
self.whiteBackground.hidden = YES;
//[self.navigationController setNavigationBarHidden:NO animated:YES];
}
//长按编辑状态
-(void)longGesture:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"保存图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"确定",@"取消", nil];
sheet.backgroundColor = [UIColor blackColor];
[sheet showInView:self.view];
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
UIImageWriteToSavedPhotosAlbum(self.imgView.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
break;
case 1:
break;
default:
break;
}
}
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
NSString *message;
if (!error) {
message = @"成功保存到相册";
} else {
message = [error description];
}
NSLog(@"message is %@",message);
}
- (void)settingView {
tempY = SCREEN_HEIGHT - 40;
self.viewSetting = [[UIView alloc]initWithFrame:CGRectMake(0, tempY, SCREEN_WIDTH, 120)];
self.viewSetting.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.viewSetting];
//屏幕亮度
UILabel *lblBrightness = [[UILabel alloc]initWithFrame:CGRectMake(10, 13, 70, 30)];
lblBrightness.font = [UIFont fontWithName:FONT_NAME1 size:FONT_SIZE2];
lblBrightness.text = @"屏幕亮度";
[self.viewSetting addSubview:lblBrightness];
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(80, 22, 210, 20)];
slider.tintColor = BACKER_COLOR;
slider.value = 0.30;/*
slider.backgroundColor = [UIColor clearColor];
[slider setMinimumTrackImage:[UIImage imageNamed:@"max.png"] forState:UIControlStateNormal];
[slider setMaximumTrackImage:[UIImage imageNamed:@"min.png"] forState:UIControlStateNormal];
*/
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[self.viewSetting addSubview:slider];
//分割线
UIView *viewLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , 0.5)];
viewLine.backgroundColor = [UIColor grayColor];
[self.viewSetting addSubview:viewLine];
UIView *viewLine2 = [[UIView alloc]initWithFrame:CGRectMake(10, 60, SCREEN_WIDTH - 20, 0.5)];
viewLine2.backgroundColor = [UIColor grayColor];
viewLine2.alpha = 0.5;
[self.viewSetting addSubview:viewLine2];
//字体大小
UILabel *lblSize = [[UILabel alloc]initWithFrame:CGRectMake(10, 75, 70, 30)];
lblSize.text = @"字体大小";
lblSize.font = [UIFont fontWithName:FONT_NAME1 size:FONT_SIZE2];
[self.viewSetting addSubview:lblSize];
NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"小字体",@"中字体",@"大字体",@"特大字",nil];
self.sengment = [[UISegmentedControl alloc]initWithItems:segmentedArray];
self.sengment.frame = CGRectMake(80, 75, 210, 30);
self.sengment.tintColor = BACKER_COLOR;
self.sengment.selectedSegmentIndex = labelSize;
[self.sengment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
[self.viewSetting addSubview:self.sengment];
}
/*
*控制屏幕亮暗度
*/
- (void)sliderAction:(UISlider *)sender {
[[UIScreen mainScreen] setBrightness:sender.value];
}
/*
*现在的位置
*/
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
lastContentOffset = scrollView.contentOffset.y;
}
-(void)swipeLeftRight:(UISwipeGestureRecognizer *)recognizer {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
@end