Просмотр исходного кода

Add webView example

Refactor most demo code, and add webView example
MJLee 11 лет назад
Родитель
Сommit
039f10e8d8

+ 16 - 0
MJRefreshExample/MJRefreshExample.xcodeproj/project.pbxproj

@@ -7,6 +7,9 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
+		2D4A99A71AB1727900828496 /* MJWebViewViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D4A99A51AB1727900828496 /* MJWebViewViewController.m */; };
+		2D4A99A81AB1727900828496 /* MJWebViewViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2D4A99A61AB1727900828496 /* MJWebViewViewController.xib */; };
+		2D4A99AB1AB178EB00828496 /* UIViewController+Example.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D4A99AA1AB178EB00828496 /* UIViewController+Example.m */; };
 		2DA7F92B1AA6B4C4005627AB /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DA7F92A1AA6B4C4005627AB /* main.m */; };
 		2DA7F9341AA6B4C4005627AB /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2DA7F9321AA6B4C4005627AB /* Main.storyboard */; };
 		2DA7F9361AA6B4C4005627AB /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2DA7F9351AA6B4C4005627AB /* Images.xcassets */; };
@@ -45,6 +48,11 @@
 /* End PBXContainerItemProxy section */
 
 /* Begin PBXFileReference section */
+		2D4A99A41AB1727900828496 /* MJWebViewViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MJWebViewViewController.h; sourceTree = "<group>"; };
+		2D4A99A51AB1727900828496 /* MJWebViewViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MJWebViewViewController.m; sourceTree = "<group>"; };
+		2D4A99A61AB1727900828496 /* MJWebViewViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = MJWebViewViewController.xib; sourceTree = "<group>"; };
+		2D4A99A91AB178EB00828496 /* UIViewController+Example.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIViewController+Example.h"; sourceTree = "<group>"; };
+		2D4A99AA1AB178EB00828496 /* UIViewController+Example.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIViewController+Example.m"; sourceTree = "<group>"; };
 		2DA7F9251AA6B4C4005627AB /* MJRefreshExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MJRefreshExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
 		2DA7F9291AA6B4C4005627AB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
 		2DA7F92A1AA6B4C4005627AB /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
@@ -177,6 +185,9 @@
 				2DA7F9601AA6BCD2005627AB /* MJTableViewController.m */,
 				2DF7EA0A1AA8C273008580B4 /* MJCollectionViewController.h */,
 				2DF7EA0B1AA8C273008580B4 /* MJCollectionViewController.m */,
+				2D4A99A41AB1727900828496 /* MJWebViewViewController.h */,
+				2D4A99A51AB1727900828496 /* MJWebViewViewController.m */,
+				2D4A99A61AB1727900828496 /* MJWebViewViewController.xib */,
 				2DF7EA0D1AA8C3DA008580B4 /* MJTestViewController.h */,
 				2DF7EA0E1AA8C3DA008580B4 /* MJTestViewController.m */,
 				2DF7EA0F1AA8C3DA008580B4 /* MJTestViewController.xib */,
@@ -188,6 +199,8 @@
 				2DF7EA081AA8A6F3008580B4 /* MJExample.m */,
 				2DA7F94F1AA6B51C005627AB /* AppDelegate.h */,
 				2DA7F9501AA6B51C005627AB /* AppDelegate.m */,
+				2D4A99A91AB178EB00828496 /* UIViewController+Example.h */,
+				2D4A99AA1AB178EB00828496 /* UIViewController+Example.m */,
 			);
 			path = Classes;
 			sourceTree = "<group>";
@@ -305,6 +318,7 @@
 			buildActionMask = 2147483647;
 			files = (
 				2DA7F9341AA6B4C4005627AB /* Main.storyboard in Resources */,
+				2D4A99A81AB1727900828496 /* MJWebViewViewController.xib in Resources */,
 				2DA7F9391AA6B4C4005627AB /* LaunchScreen.xib in Resources */,
 				2DA7F96F1AA75076005627AB /* MJRefresh.bundle in Resources */,
 				2DF7EA111AA8C3DA008580B4 /* MJTestViewController.xib in Resources */,
@@ -329,6 +343,7 @@
 				2DA7F9671AA6BDC9005627AB /* MJRefreshLegendHeader.m in Sources */,
 				2DA7F97E1AA8389C005627AB /* MJRefreshLegendFooter.m in Sources */,
 				2DA7F9581AA6B57A005627AB /* UIScrollView+MJRefresh.m in Sources */,
+				2D4A99A71AB1727900828496 /* MJWebViewViewController.m in Sources */,
 				2DA7F95B1AA6B5F1005627AB /* MJRefreshHeader.m in Sources */,
 				2DA7F9541AA6B51C005627AB /* AppDelegate.m in Sources */,
 				2DF7EA101AA8C3DA008580B4 /* MJTestViewController.m in Sources */,
@@ -343,6 +358,7 @@
 				2DA7F9741AA759BC005627AB /* UIScrollView+MJExtension.m in Sources */,
 				2DA7F9611AA6BCD2005627AB /* MJTableViewController.m in Sources */,
 				2DA7F95E1AA6BC2C005627AB /* MJRefreshGifHeader.m in Sources */,
+				2D4A99AB1AB178EB00828496 /* UIViewController+Example.m in Sources */,
 				2DA7F97B1AA832F1005627AB /* MJRefreshFooter.m in Sources */,
 				2DF7EA0C1AA8C273008580B4 /* MJCollectionViewController.m in Sources */,
 			);

+ 1 - 1
MJRefreshExample/MJRefreshExample/Classes/MJCollectionViewController.h

@@ -10,5 +10,5 @@
 #import <UIKit/UIKit.h>
 
 @interface MJCollectionViewController : UICollectionViewController
-@property (copy, nonatomic) NSString *method;
+
 @end

+ 1 - 4
MJRefreshExample/MJRefreshExample/Classes/MJCollectionViewController.m

@@ -9,6 +9,7 @@
 
 #import "MJCollectionViewController.h"
 #import "MJTestViewController.h"
+#import "UIViewController+Example.h"
 #import "MJRefresh.h"
 
 static const CGFloat MJDuration = 2.0;
@@ -77,10 +78,6 @@ - (NSMutableArray *)colors
 }
 
 #pragma mark - 其他
-- (void)dealloc
-{
-    MJLog(@"%@销毁了", [self class]);
-}
 
 /**
  *  初始化

+ 1 - 0
MJRefreshExample/MJRefreshExample/Classes/MJExample.h

@@ -13,4 +13,5 @@
 @property (copy, nonatomic) NSString *header;
 @property (strong, nonatomic) NSArray *titles;
 @property (strong, nonatomic) NSArray *methods;
+@property (assign, nonatomic) Class vcClass;
 @end

+ 18 - 17
MJRefreshExample/MJRefreshExample/Classes/MJExampleViewController.m

@@ -9,12 +9,15 @@
 
 #import "MJExampleViewController.h"
 #import "MJTableViewController.h"
+#import "MJWebViewViewController.h"
 #import "MJCollectionViewController.h"
 #import "MJExample.h"
+#import "UIViewController+Example.h"
 
 static NSString *const MJExample00 = @"UITableView + 下拉刷新";
 static NSString *const MJExample10 = @"UITableView + 上拉刷新";
 static NSString *const MJExample20 = @"UICollectionView";
+static NSString *const MJExample30 = @"UIWebView";
 
 @interface MJExampleViewController()
 @property (strong, nonatomic) NSArray *examples;
@@ -27,20 +30,29 @@ - (NSArray *)examples
     if (!_examples) {
         MJExample *exam0 = [[MJExample alloc] init];
         exam0.header = MJExample00;
+        exam0.vcClass = [MJTableViewController class];
         exam0.titles = @[@"传统", @"动画图片", @"隐藏时间", @"隐藏状态和时间01", @"隐藏状态和时间02", @"自定义文字"];
         exam0.methods = @[@"example01", @"example02", @"example03", @"example04", @"example05", @"example06"];
         
         MJExample *exam1 = [[MJExample alloc] init];
         exam1.header = MJExample10;
+        exam1.vcClass = [MJTableViewController class];
         exam1.titles = @[@"传统(尝试向上拽)", @"动画图片", @"隐藏状态01", @"隐藏状态02", @"全部加载完毕", @"禁止自动加载", @"自定义文字", @"加载后隐藏"];
         exam1.methods = @[@"example11", @"example12", @"example13", @"example14", @"example15", @"example16", @"example17", @"example18"];
         
         MJExample *exam2 = [[MJExample alloc] init];
         exam2.header = MJExample20;
+        exam2.vcClass = [MJCollectionViewController class];
         exam2.titles = @[@"上下拉刷新"];
         exam2.methods = @[@"example21"];
         
-        self.examples = @[exam0, exam1, exam2];
+        MJExample *exam3 = [[MJExample alloc] init];
+        exam3.header = MJExample30;
+        exam3.vcClass = [MJWebViewViewController class];
+        exam3.titles = @[@"下拉刷新"];
+        exam3.methods = @[@"example31"];
+        
+        self.examples = @[exam0, exam1, exam2, exam3];
     }
     return _examples;
 }
@@ -64,11 +76,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
     MJExample *exam = self.examples[indexPath.section];
     cell.textLabel.text = exam.titles[indexPath.row];
     
-    if ([exam.header isEqualToString:MJExample20]) { // UICollectionView
-        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", [MJCollectionViewController class], exam.methods[indexPath.row]];
-    } else { // UITableView
-        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", [MJTableViewController class], exam.methods[indexPath.row]];
-    }
+    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", exam.vcClass, exam.methods[indexPath.row]];
     
     return cell;
 }
@@ -82,17 +90,10 @@ - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInte
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
     MJExample *exam = self.examples[indexPath.section];
-    if ([exam.header isEqualToString:MJExample20]) { // UICollectionView
-        MJCollectionViewController *collectVc = [[MJCollectionViewController alloc] init];
-        collectVc.title = exam.titles[indexPath.row];
-        collectVc.method = exam.methods[indexPath.row];
-        [self.navigationController pushViewController:collectVc animated:YES];
-    } else { // UITableView
-        MJTableViewController *tableVc = [[MJTableViewController alloc] init];
-        tableVc.title = exam.titles[indexPath.row];
-        tableVc.method = exam.methods[indexPath.row];
-        [self.navigationController pushViewController:tableVc animated:YES];
-    }
+    UIViewController *vc = [[exam.vcClass alloc] init];
+    vc.title = exam.titles[indexPath.row];
+    [vc setValue:exam.methods[indexPath.row] forKeyPath:@"method"];
+    [self.navigationController pushViewController:vc animated:YES];
 }
 
 @end

+ 1 - 1
MJRefreshExample/MJRefreshExample/Classes/MJTableViewController.h

@@ -10,5 +10,5 @@
 #import <UIKit/UIKit.h>
 
 @interface MJTableViewController : UITableViewController
-@property (copy, nonatomic) NSString *method;
+
 @end

+ 2 - 6
MJRefreshExample/MJRefreshExample/Classes/MJTableViewController.m

@@ -8,9 +8,10 @@
 //
 
 #import "MJTableViewController.h"
-#import "MJRefresh.h"
 #import "UIView+MJExtension.h"
 #import "MJTestViewController.h"
+#import "UIViewController+Example.h"
+#import "MJRefresh.h"
 
 static const CGFloat MJDuration = 2.0;
 /**
@@ -499,9 +500,4 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
     }
 }
 
-- (void)dealloc
-{
-    MJLog(@"%@销毁了", [self class]);
-}
-
 @end

+ 13 - 0
MJRefreshExample/MJRefreshExample/Classes/MJWebViewViewController.h

@@ -0,0 +1,13 @@
+//
+//  MJWebViewViewController.h
+//  MJRefreshExample
+//
+//  Created by MJ Lee on 15/3/12.
+//  Copyright (c) 2015年 itcast. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@interface MJWebViewViewController : UIViewController
+
+@end

+ 71 - 0
MJRefreshExample/MJRefreshExample/Classes/MJWebViewViewController.m

@@ -0,0 +1,71 @@
+//
+//  MJWebViewViewController.m
+//  MJRefreshExample
+//
+//  Created by MJ Lee on 15/3/12.
+//  Copyright (c) 2015年 itcast. All rights reserved.
+//
+
+#import "MJWebViewViewController.h"
+#import "UIViewController+Example.h"
+#import "MJRefresh.h"
+
+@interface MJWebViewViewController ()
+@property (weak, nonatomic) IBOutlet UIWebView *webView;
+@end
+
+@implementation MJWebViewViewController
+#pragma mark - 示例
+- (void)example31
+{
+    __weak UIScrollView *scrollView = self.webView.scrollView;
+    
+    // 添加下拉刷新控件
+    [scrollView addLegendHeaderWithRefreshingBlock:^{
+        MJLog(@"进入下拉刷新");
+        
+        // 模仿2秒后刷新成功
+        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+            // 结束刷新
+            [scrollView.header endRefreshing];
+            MJLog(@"结束刷新");
+        });
+    }];
+    
+    // 如果是上拉刷新,就以此类推
+}
+
+#pragma mark - 其他
+- (void)viewDidLoad {
+    [super viewDidLoad];
+    
+    // 加载页面
+    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://weibo.com/exceptions"]]];
+    
+    [self performSelector:NSSelectorFromString(self.method) withObject:nil];
+}
+
+- (void)viewWillAppear:(BOOL)animated
+{
+    [super viewWillAppear:animated];
+    
+    [self.navigationController setNavigationBarHidden:YES animated:YES];
+}
+
+- (void)viewWillDisappear:(BOOL)animated
+{
+    [super viewWillDisappear:animated];
+    
+    [self.navigationController setNavigationBarHidden:NO animated:YES];
+}
+
+- (BOOL)prefersStatusBarHidden
+{
+    return YES;
+}
+
+- (IBAction)back {
+    [self.navigationController popViewControllerAnimated:YES];
+}
+
+@end

+ 61 - 0
MJRefreshExample/MJRefreshExample/Classes/MJWebViewViewController.xib

@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6250" systemVersion="14D87h" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
+    <dependencies>
+        <deployment identifier="iOS"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6244"/>
+    </dependencies>
+    <objects>
+        <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="MJWebViewViewController">
+            <connections>
+                <outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
+                <outlet property="webView" destination="2Of-d4-HFt" id="PE7-tq-AIx"/>
+            </connections>
+        </placeholder>
+        <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
+        <view clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="i5M-Pr-FkT">
+            <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
+            <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
+            <subviews>
+                <webView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="2Of-d4-HFt">
+                    <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
+                    <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
+                </webView>
+                <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="8xo-5D-MRC">
+                    <rect key="frame" x="466" y="470" width="113" height="30"/>
+                    <color key="backgroundColor" red="1" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
+                    <constraints>
+                        <constraint firstAttribute="width" constant="113" id="2YR-NZ-hF1"/>
+                        <constraint firstAttribute="height" constant="30" id="qIc-RV-DwB"/>
+                    </constraints>
+                    <state key="normal" title="回到上一页">
+                        <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
+                    </state>
+                    <connections>
+                        <action selector="back" destination="-1" eventType="touchUpInside" id="BG2-eT-gw7"/>
+                    </connections>
+                </button>
+                <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="注意,这不是原生界面,是个网页:http://weibo.com/excepptions" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="2VR-jQ-Qn4">
+                    <rect key="frame" x="371" y="392.5" width="208" height="31.5"/>
+                    <color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.16" colorSpace="custom" customColorSpace="sRGB"/>
+                    <constraints>
+                        <constraint firstAttribute="width" constant="208" id="qkq-G9-eo2"/>
+                    </constraints>
+                    <fontDescription key="fontDescription" type="system" pointSize="13"/>
+                    <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
+                    <nil key="highlightedColor"/>
+                </label>
+            </subviews>
+            <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
+            <constraints>
+                <constraint firstItem="2Of-d4-HFt" firstAttribute="top" secondItem="i5M-Pr-FkT" secondAttribute="top" id="3LR-sb-d2d"/>
+                <constraint firstAttribute="bottom" secondItem="8xo-5D-MRC" secondAttribute="bottom" constant="100" id="GEd-8N-H2g"/>
+                <constraint firstAttribute="trailing" secondItem="2VR-jQ-Qn4" secondAttribute="trailing" constant="21" id="JkD-lQ-2rZ"/>
+                <constraint firstAttribute="trailing" secondItem="2Of-d4-HFt" secondAttribute="trailing" id="MWE-Gj-9Jb"/>
+                <constraint firstAttribute="bottom" secondItem="2Of-d4-HFt" secondAttribute="bottom" id="YY5-j2-abg"/>
+                <constraint firstAttribute="trailing" secondItem="8xo-5D-MRC" secondAttribute="trailing" constant="21" id="spl-mb-Eed"/>
+                <constraint firstItem="8xo-5D-MRC" firstAttribute="top" secondItem="2VR-jQ-Qn4" secondAttribute="bottom" constant="46" id="zTm-ax-FFk"/>
+                <constraint firstItem="2Of-d4-HFt" firstAttribute="leading" secondItem="i5M-Pr-FkT" secondAttribute="leading" id="zy2-dj-Ccd"/>
+            </constraints>
+        </view>
+    </objects>
+</document>

+ 13 - 0
MJRefreshExample/MJRefreshExample/Classes/UIViewController+Example.h

@@ -0,0 +1,13 @@
+//
+//  UIViewController+Example.h
+//  MJRefreshExample
+//
+//  Created by MJ Lee on 15/3/12.
+//  Copyright (c) 2015年 itcast. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@interface UIViewController (Example)
+@property (copy, nonatomic) NSString *method;
+@end

+ 39 - 0
MJRefreshExample/MJRefreshExample/Classes/UIViewController+Example.m

@@ -0,0 +1,39 @@
+//
+//  UIViewController+Example.m
+//  MJRefreshExample
+//
+//  Created by MJ Lee on 15/3/12.
+//  Copyright (c) 2015年 itcast. All rights reserved.
+//
+
+#import "UIViewController+Example.h"
+#import <objc/runtime.h>
+
+@implementation UIViewController (Example)
+
+#pragma mark - swizzle
++ (void)load
+{
+    Method method1 = class_getInstanceMethod([self class], NSSelectorFromString(@"dealloc"));
+    Method method2 = class_getInstanceMethod([self class], @selector(deallocSwizzle));
+    method_exchangeImplementations(method1, method2);
+}
+
+- (void)deallocSwizzle
+{
+    NSLog(@"%@被销毁了", self);
+    
+    [self deallocSwizzle];
+}
+
+static char MethodKey;
+- (void)setMethod:(NSString *)method
+{
+    objc_setAssociatedObject(self, &MethodKey, method, OBJC_ASSOCIATION_COPY_NONATOMIC);
+}
+
+- (NSString *)method
+{
+    return objc_getAssociatedObject(self, &MethodKey);
+}
+@end