IOS Development--imitation news home effect Wmpagecontroller The use of detailed _ios

Source: Internet
Author: User
Tags arrays


This record is the use of the third party library Wmpagecontroller control in iOS development, mainly used to page content, you can move through gestures to switch pages, or by clicking on the title section to switch pages, as shown in the following image:



How to use:



Create a new project DemoTest1 and then introduce Wmpagecontroller to the project via Cocoapods, podfile the contents of the file are as follows:


Platform:ios, ' 7.0 '

target ' DemoTest1 ' do

 pod ' wmpagecontroller ', ' ~> 1.6.4 '

end


Method One:



(1) Create several Viewcontroller inherits from Uiviewcontroller test:



(2) Open the APPDELEGATE.M file and add one of the following methods to it:


#import "WMPageController.h"

#import "OneViewController.h"
#import "ViewController.h"
#import "TwoViewController.h"

@interface AppDelegate ()

@property (nonatomic, strong) WMPageController * createPages;

@end

@implementation AppDelegate

-(WMPageController *) createPages {


  // Array of pages contained in WMPageController
  NSArray * controllers = [NSArray arrayWithObjects: [ViewController class], [OneViewController class], [TwoViewController class], nil];
  // WMPageController title array
  NSArray * titles = [NSArray arrayWithObjects: @ "体育 新闻", @ "娱乐 新闻", @ "直播 新闻", nil];
  // Initialize the WMPageController object with the above two arrays
  WMPageController * pageController = [[WMPageController alloc] initWithViewControllerClasses: controllers andTheirTitles: titles];
  pageController.menuViewStyle = WMMenuViewStyleLine;
  pageController.titleColorSelected = [UIColor whiteColor];
  pageController.titleColorNormal = [UIColor colorWithRed: 168.0 / 255.0 green: 20.0 / 255.0 blue: 4 / 255.0 alpha: 1];
  pageController.progressColor = [UIColor colorWithRed: 168.0 / 255.0 green: 20.0 / 255.0 blue: 4 / 255.0 alpha: 1];
  // pageController.itemsWidths = @ [@ (70), @ (100), @ (70)]; // You can set different widths here

  // Set the width of each title of WMPageController
  pageController.menuItemWidth = 375/3;
  // Set the height of the WMPageController title bar
  pageController.menuHeight = 35;
  // Set the color of the title selected by WMPageController
  pageController.titleColorSelected = [UIColor colorWithRed: 200 green: 0 blue: 0 alpha: 1];
  return pageController;
}


-(BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {


 self.window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen] .bounds];


  WMPageController * page = [self createPages];

  UINavigationController * na = [[UINavigationController alloc] initWithRootViewController: page];

  self.window.rootViewController = na;

  [self.window makeKeyAndVisible];

  return YES;
}


Method Two:



(1) Create a viewcontroller inherit from Wmpagecontroller



(2) Edit the viewcontroller.m file with the following code:



Initialization settings:


#import "TwoViewController.h"
#import "OneViewController.h"
#import "TableViewController.h"

#import " ThreeViewController.h "

@interface threeviewcontroller () 

@property (nonatomic, strong) Nsarray *titledata;
@end

@implementation threeviewcontroller

#pragma mark title array
-(Nsarray *) Titledata {
  if (!_titledata) {
    _titledata = @[@ "single", @ "Details", @ "lyrics", @ "lyrics"];
  }
  return _titledata;
}
#pragma mark initialization Code
-(instancetype) init {
  if (self = [super init]) {


    self.titlesizenormal =;
    self.titlesizeselected =;
    Self.menuviewstyle = Wmmenuviewstyleline;
    Self.menuitemwidth = [UIScreen mainscreen].bounds.size.width/self.titledata.count;
    Self.menuheight = M
  return self;
}


Wmpagecontroller--datasource & Delegate


#pragma mark-Datasource & Delegate

#pragma mark Returns the number of child pages
-(NSInteger) numbersOfChildControllersInPageController: (WMPageController *) pageController {
  return self.titleData.count;
}

#pragma mark Returns the page corresponding to an index
-(UIViewController *) pageController: (WMPageController *) pageController viewControllerAtIndex: (NSInteger) index {


  switch (index) {
      case 0: {

        TwoViewController * vcClass = [[TwoViewController alloc] init];
        vcClass.title = @ "1";

        return vcClass;
      }

        break;
      case 1: {

        OneViewController * vcClass = [OneViewController new];
        vcClass.title = @ "2";
        return vcClass;

      }
        break;
      case 2: {

        TableViewController * vcClass = [TableViewController new];
        vcClass.title = @ "3";
        return vcClass;

      }
        break;

      default: {

        OneViewController * vcClass = [OneViewController new];
        vcClass.title = @ "4";
        return vcClass;

      }

        break;
    }


}

#pragma mark Returns the title corresponding to index
-(NSString *) pageController: (WMPageController *) pageController titleAtIndex: (NSInteger) index {

  return self.titleData [index];
}


Related settings:


/ **
 * Values and keys can set properties when initialize child controlelr (it's KVC)
 * The values keys property can be used to pass values to the controller when initializing the controller (using KVC to set)
  When using, please make sure that the key is consistent with the property name of the controller! !! (For example: the controller has a property type that needs to be set, then the keys are the string @ "type")
 * /
@property (nonatomic, strong) NSMutableArray <id> * values;
@property (nonatomic, strong) NSMutableArray <NSString *> * keys;

/ **
 * The class of each controller, for example: [UITableViewController class]
 * Each controller's class, example: [UITableViewController class]
 * /
@property (nonatomic, copy) NSArray <Class> * viewControllerClasses;

/ **
 * Each controller title
 * Titles of view controllers in page controller.
 * /
@property (nonatomic, copy) NSArray <NSString *> * titles;
@property (nonatomic, strong, readonly) UIViewController * currentViewController;

/ **
 * Set the selected item number
 * To select item at index
 * /
@property (nonatomic, assign) int selectIndex;

/ **
 * Whether clicking on the adjacent MenuItem triggers a page turning animation
 * Whether to animate when press the MenuItem, if distant between the selected and the pressed is larger than 1, never animate.
 * /
@property (nonatomic, assign) BOOL pageAnimatable;

/ **
 * Title size when selected
 * The title size when selected (animatable)
 * /
@property (nonatomic, assign) CGFloat titleSizeSelected;

/ **
 * Title size when unselected
 * The normal title size (animatable)
 * /
@property (nonatomic, assign) CGFloat titleSizeNormal;

/ **
 * The color when the title is selected, the color is animable.
 * The title color when selected, the color is animatable.
 * /
@property (nonatomic, strong) UIColor * titleColorSelected;

/ **
 * The color of the title is not selected, the color is animated.
 * The title's normal color, the color is animatable.
 * /
@property (nonatomic, strong) UIColor * titleColorNormal;

/ **
 * Title font name
 * The name of title's font
 * /
@property (nonatomic, copy) NSString * titleFontName;

/ **
 * Navigation bar height
 * The menu view's height
 * /
@property (nonatomic, assign) CGFloat menuHeight;

// When the widths of all items add up to less than the screen width, PageController will automatically help with typesetting, adding a gap between each item to fill the entire width
// When the sum of all the item's width is smaller than the screen's width, pageController will add gap to each item automatically, in order to fill the width.

/ **
 * The width of each MenuItem
 * The item width, when all are same, use this property
 * /
@property (nonatomic, assign) CGFloat menuItemWidth;

/ **
 * The width of each MenuItem can vary. The array is NSNumber.
 * Each item's width, when they are not all the same, use this property, Put `NSNumber` in this array.
 * /
@property (nonatomic, copy) NSArray <NSNumber *> * itemsWidths;

/ **
 * Navigation bar background color
 * The background color of menu view
 * /
@property (nonatomic, strong) UIColor * menuBGColor;

/ **
 * Menu view style, default is no underline
 * Menu view's style, now has two different styles, 'Line', 'default'
 * /
@property (nonatomic, assign) WMMenuViewStyle menuViewStyle;

@property (nonatomic, assign) WMMenuViewLayoutMode menuViewLayoutMode;

/ **
 * The color of the progress bar, the default is the same as the selected color (if style is Default, this property is useless)
 * The progress's color, the default color is the same with `titleColorSelected`. If you want to have a different color, set this property.
 * /
@property (nonatomic, strong) UIColor * progressColor;

/ **
 * Customize the width of the progress bar under each item
 * /
@property (nonatomic, strong) NSArray * progressViewWidths;

/// Customize the progress bar, if each progress bar is the same length, you can set this property
@property (nonatomic, assign) CGFloat progressWidth;

/// Naughty effect, used to realize the new effect of Tencent video, please set a smaller progressWidth
@property (nonatomic, assign) BOOL progressViewIsNaughty;

/ **
 * Whether to send the notification to the observer when the controller is created or the view is completely displayed in front of the user. The default is not enabled. If you want to use the notification, please enable
 * Whether notify observer when finish init or fully displayed to user, the default is NO.
 * See `WMPageConst.h` for more information.
 * /
@property (nonatomic, assign) BOOL postNotification;

/ **
 * Whether to record the position of the Controller and return to the corresponding position when returning next time. The default is NO (it will not trigger if it exists in the current cache)
 * Whether to remember controller's positon if it's a kind of scrollView controller, like UITableViewController, The default value is NO.
 * For example, `UITabelViewController`, of course, you can also set it in your own controller, if you replace Controller.view with scrollView or add a scrollView with the same bounds as Controller.view
 * /
@property (nonatomic, assign) BOOL rememberLocation __deprecated_msg ("Because of the cache policy, this property can abondon now.");

/ ** cache mechanism, default is unlimited (if you receive a memory warning, it will automatically switch) * /
@property (nonatomic, assign) WMPageControllerCachePolicy cachePolicy;

/ ** Preload mechanism, n pages are preloaded when the slide stops * /
@property (nonatomic, assign) WMPageControllerPreloadPolicy preloadPolicy;

/ ** Whether ContentView bounces * /
@property (nonatomic, assign) BOOL bounces;

/ **
 * Whether to display as titleView of NavigationBar, default NO
 * Whether to show on navigation bar, the default value is `NO`
 * /
@property (assign, nonatomic) BOOL showOnNavigationBar;

/ **
 * Before setting contentOffset of contentView with code, please set startDragging = YES
 * Set startDragging = YES before set contentView.contentOffset = xxx;
 * /
@property (nonatomic, assign) BOOL startDragging;

/ ** Height of the underlined progress bar * /
@property (nonatomic, assign) CGFloat progressHeight;

/ ** WMPageController View 'frame * /
@property (nonatomic, assign) CGRect viewFrame;

/ **
 * Menu view items 'margin / make sure it's count is equal to (controllers' count + 1), default is 0
  The gap of each item in the top menu bar, because it includes the head and tail ends, so make sure its number equals the number of controllers + 1, the default gap is 0
 * /
@property (nonatomic, copy) NSArray <NSNumber *> * itemsM
argins;

/ **
 * set itemMargin if all margins are the same, default is 0
  If the gaps are the same, set this property. The default is 0.
 * /
@property (nonatomic, assign) CGFloat itemMargin;

/ ** Gap between top menuView and scrollView * /
@property (nonatomic, assign) CGFloat menuViewBottomSpace;

/ ** distance from progressView to the bottom of menuView * /
@property (nonatomic, assign) CGFloat progressViewBottomSpace;

/ ** progressView's cornerRadius * /
@property (nonatomic, assign) CGFloat progressViewCornerRadius;
/ ** Top navigation bar * /
@property (nonatomic, weak) WMMenuView * menuView;

/ ** Internal container * /
@property (nonatomic, weak) WMScrollView * scrollView;

/ ** MenuView distance from left to right * /
@property (nonatomic, assign) CGFloat menuViewContentMargin;

/ **
 * When swiping left, other gestures are enabled at the same time, such as swiping left of the system and swiping left of sidemenu. Default NO
  (It will cause a small problem, the first and last controllers will become slidable, which has not been solved yet.)
 * /
@property (assign, nonatomic) BOOL otherGestureRecognizerSimultaneously;
/ **
 * Construction method, please use this method to create the controller. Or implement the data source method. /
 * Init method, recommended to use this instead of `-init`. Or you can implement datasource by yourself.
 *
 * @param classes The class of the child controller, make sure the number is equal to the number of titles
 * @param titles The title of each child controller, described by NSString
 *
 * @return instancetype
 * /
-(instancetype) initWithViewControllerClasses: (NSArray <Class> *) classes andTheirTitles: (NSArray <NSString *> *) titles;

/ **
 * A method in order to reload MenuView and child view controllers. If you had set `itemsMargins` or` itemsWidths` `values` and` keys` before, make sure you have update them also before you call this method. And most important, PAY ATTENTION TO THE COUNT OF THOSE ARRAY.
  This method is used to reset the refresh parent controller, which includes the top MenuView and childViewControllers. If the `itemsMargins` and` itemsWidths` `values` and` keys` properties have been set before, please ensure that these are also updated before calling reload Attributes. And, most of all, pay attention to the number of arrays to prevent overflow.
 * /
-(void) reloadData;

/ **
 * Update designated item's title
  Update the title of the controller with the specified sequence number
 *
 * @param title new title
 * @param index target sequence number
 * /
-(void) updateTitle: (NSString *) title atIndex: (NSInteger) index;

/ **
 * Update designated item's title and width
  Update the title of the controller with the specified serial number and its width
 *
 * @param title new title
 * @param index target sequence number
 * @param width corresponds to the new width of the item
 * /
-(void) updateTitle: (NSString *) title andWidth: (CGFloat) width atIndex: (NSInteger) index;

/ ** Notification received when the app is about to enter the background * /
-(void) willResignActive: (NSNotification *) notification;
/ ** Notification received when the app is about to return to the foreground * /
-(void) willEnterForeground: (NSNotification *) notification; 


SOURCE Demo: Source download



The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.