webview添加头尾视图

在一些应用里面需要在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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值