在一些应用里面需要在webview一些在头部或者尾部添加一些视图
#import <Foundation/Foundation.h>
@interface LWebView : UIWebView {
UIScrollView *_scrollView;
UIView *headerView;
UIView *footerView;
}
@property (nonatomic,retain) UIView *footerView;
@property (nonatomic,retain) UIView *headerView;
- (void) headerViewHeightChange:(int)height animated:(BOOL)animated;
- (id) initWithFrame:(CGRect)aFrame;
@end
#import "LWebView.h"
CG_INLINE CGRect
CGRectSetWidth(CGRect rect,CGFloat width)
{
rect.size.width = width;
return rect;
}
CG_INLINE CGRect
CGRectSetHeight(CGRect rect,CGFloat height)
{
rect.size.height = height;
return rect;
}
CG_INLINE CGRect
CGRectSetX(CGRect rect,CGFloat x)
{
rect.origin.x = x;
return rect;
}
CG_INLINE CGRect
CGRectSetY(CGRect rect,CGFloat y)
{
rect.origin.y = y;
return rect;
}
CG_INLINE UIEdgeInsets
UIEdgeInsetsSetTop(UIEdgeInsets insets,CGFloat top)
{
insets.top = top;
return insets;
}
CG_INLINE UIEdgeInsets
UIEdgeInsetsSetLeft(UIEdgeInsets insets,CGFloat left)
{
insets.left = left;
return insets;
}
CG_INLINE UIEdgeInsets
UIEdgeInsetsSetBottom(UIEdgeInsets insets,CGFloat bottom)
{
insets.bottom = bottom;
return insets;
}
CG_INLINE UIEdgeInsets
UIEdgeInsetsSetRight(UIEdgeInsets insets,CGFloat right)
{
insets.right = right;
return insets;
}
@implementation LWebView
@synthesize headerView, footerView;
- (id) initWithFrame:(CGRect)aFrame
{
if (self = [superinitWithFrame:aFrame]) {
for (id subviewin self.subviews){
if ([[subviewclass] isSubclassOfClass: [UIScrollViewclass]]) {
_scrollView = ((UIScrollView *)subview);
}
}
}
returnself;
}
- (void) setFooterView:(UIView *)view
{
if (footerView) {
[footerViewremoveFromSuperview];
[footerViewrelease], footerView =nil;
}
footerView = view;
footerView.frame =CGRectSetY(footerView.frame,_scrollView.contentSize.height);
_scrollView.contentInset =UIEdgeInsetsSetBottom(_scrollView.contentInset, view.frame.size.height);
[_scrollViewaddSubview:footerView];
}
- (void) setHeaderView:(UIView *)view
{
if (headerView) {
[headerViewremoveFromSuperview];
[headerViewrelease], headerView =nil;
}
headerView = view;
headerView.frame =CGRectSetY(headerView.frame, -headerView.frame.size.height);
_scrollView.contentInset =UIEdgeInsetsSetTop(_scrollView.contentInset,headerView.frame.size.height);
_scrollView.contentOffset =CGPointMake(0, -headerView.frame.size.height);
[_scrollViewaddSubview:headerView];
}
- (void) headerViewHeightChange:(int)height animated:(BOOL)animated
{
if (animated) {
[UIViewbeginAnimations:@"animateTableView"context:nil];
[UIViewsetAnimationDuration:0.3];
}
_scrollView.contentOffset =CGPointMake(_scrollView.contentOffset.x, -height);
_scrollView.contentInset =UIEdgeInsetsSetTop(_scrollView.contentInset, height);
self.headerView.frame =CGRectMake(self.headerView.frame.origin.x, -height, self.headerView.frame.size.width, height);
if (animated) {
[UIViewcommitAnimations];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
self.footerView.frame =CGRectSetY(footerView.frame,_scrollView.contentSize.height);
int webViewWidth =self.frame.size.width;
//缩小到小于webview宽度时
if (scrollView.contentSize.width < webViewWidth) {
CGSize contentSize = scrollView.contentSize;
contentSize.width = webViewWidth;
scrollView.contentSize = contentSize;
}
//左右露边
if (scrollView.contentSize.width - webViewWidth <0 && scrollView.contentOffset.x <0) {
self.headerView.frame =CGRectSetX(self.headerView.frame, scrollView.contentOffset.x);
self.footerView.frame =CGRectSetX(self.footerView.frame, scrollView.contentOffset.x);
}
//左露边
elseif (scrollView.contentOffset.x <0) {
self.headerView.frame =CGRectSetX(self.headerView.frame,0);
self.footerView.frame =CGRectSetX(self.footerView.frame,0);
}
//右露边
elseif (scrollView.contentOffset.x > scrollView.contentSize.width - webViewWidth) {
self.headerView.frame =CGRectSetX(self.headerView.frame, scrollView.contentSize.width - webViewWidth);
self.footerView.frame =CGRectSetX(self.footerView.frame, scrollView.contentSize.width - webViewWidth);
}
//平常滚动/缩放
else {
self.headerView.frame =CGRectSetX(self.headerView.frame, scrollView.contentOffset.x);
self.footerView.frame =CGRectSetX(self.footerView.frame, scrollView.contentOffset.x);
}
}
@end