日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ios中MKHorizMenu用法

發布時間:2025/4/16 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios中MKHorizMenu用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下載地址 https://github.com/MugunthKumar/MKHorizMenuDemo
直接 加入MKHorizMenu目錄即可
下載包地址 http://pan.baidu.com/share/link?shareid=2872269045&uk=923776187
//
// MyViewController.m // MyTest // // Created by zy on 13-8-1. // Copyright (c) 2013年 zy. All rights reserved. // #import "MyViewController.h" #import "JSContentView.h"@interface MyViewController (){JSContentView *newView;UIView *contentview; }@property(nonatomic,retain)NSArray *mydata; //緩存view @property(nonatomic,retain)NSMutableDictionary *cacheDic; @end@implementation MyViewController- (void)viewDidLoad {[super viewDidLoad];self.cacheDic=[NSMutableDictionary dictionary];self.view.backgroundColor=[UIColor whiteColor];self.mydata=@[@"語文",@"數學",@"英語"];MKHorizMenu *Menu=[[MKHorizMenu alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 80)];Menu.dataSource=self;Menu.itemSelectedDelegate=self;contentview=[[UIView alloc] initWithFrame:CGRectMake(0,90, 320, 300)];//內容view, [self.view addSubview:contentview];[contentview release];[self.view addSubview:Menu];[self horizMenu:Menu itemSelectedAtIndex:0];//默認是第一個選中 [Menu reloadData];[Menu release]; }#pragma mark -MKHorizMenu DataSource- (UIColor*) backgroundColorForMenu:(MKHorizMenu*) tabView{return [UIColor purpleColor]; } - (int) numberOfItemsForMenu:(MKHorizMenu*) tabView{return 3; }- (NSString*) horizMenu:(MKHorizMenu*) horizMenu titleForItemAtIndex:(NSUInteger) index{return self.mydata[index]; }#pragma mark -MK Delegate - (void)horizMenu:(MKHorizMenu*) horizMenu itemSelectedAtIndex:(NSUInteger) index{for (UIView *child in contentview.subviews) {//先移除所有的view. [child removeFromSuperview];}NSString *tempstr=self.mydata[index];if(![self.cacheDic objectForKey:tempstr]){switch (index) {case 0:{newView=[[JSContentView alloc] initWithFrame:contentview.bounds requestSEL:@selector(Requestdeta) aDelegate:self];[contentview addSubview:newView];[self.cacheDic setObject:newView forKey:tempstr];//放入緩存中 [newView release];}break;case 1:{UIView *view=[[UIView alloc] initWithFrame:contentview.bounds];view.backgroundColor=[UIColor redColor];[contentview addSubview:view];[self.cacheDic setObject:view forKey:tempstr];[view release];}break;case 2:{UIView *view=[[UIView alloc] initWithFrame:contentview.bounds];view.backgroundColor=[UIColor yellowColor];[contentview addSubview:view];[self.cacheDic setObject:view forKey:tempstr];[view release];}break;default:break;}}else{UIView *view=[self.cacheDic objectForKey:tempstr];[contentview addSubview:view];}}-(void)Requestdeta{NSString *url=[NSString stringWithFormat:@"http://61.177.61.252/szzssw/taxCommon.action?code=1001&pagecode=%zi&menucode=3300000000",newView.Pagecode==0?1:newView.Pagecode];//請求數據,重新刷新數據[[JSNetWord ShareNetwork] NetWorkWithConnctId:100 body:url PostBody:nil aDelegate:self];//先從緩存中讀取NSString *cache=[self readApiCacheFileName:@"1.txt"];if (cache.length>0 &&newView.Pagecode==1) {[self NetWorkWithConnectId:100 aBackString:cache withState:0];}}-(void)NetWorkWithConnectId:(int)connectid aBackString:(NSString *)backString withState:(int)state{if(connectid==100){if (newView.Pagecode<=1 && state==1) {//是下拉newView.tableview.pullTableIsRefreshing=NO;}if (newView.Pagecode>1) {//上拉newView.tableview.pullTableIsLoadingMore=NO;}if (newView.Pagecode==1) {//下拉 保存數據[self saveApiCacheFileName:@"1.txt" textContent:backString Append:NO];}NSDictionary *arr= [backString JSONValue];if (newView.Pagecode==1 &&newView.mydata.count>0) {[newView.mydata removeAllObjects];//下拉 只顯示20 ,上拉 累加 }[newView.mydata addObjectsFromArray:arr[@"content"]];[newView.tableview reloadData];} }- (void)dealloc {[_mydata release];[_cacheDic release];[super dealloc]; }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }@end

第一種方式 ??修改 MKHorizMenu.m文件

-(void) reloadData {NSArray *viewsToRemove = [self subviews];for (UIView *v in viewsToRemove) {[v removeFromSuperview];}self.itemCount = [dataSource numberOfItemsForMenu:self];//調用代理的方法float btnWidth = self.bounds.size.width / self.itemCount;//得到寬度int tag = kButtonBaseTag;self.backgroundColor = [dataSource backgroundColorForMenu:self];// self.selectedImage = [dataSource selectedItemImageForMenu:self]; 去掉代理這個方法 CGRect imgRec,btnRec,hRec;CGRectDivide(self.bounds, &imgRec, &btnRec, 2, CGRectMaxYEdge);for(int i = 0 ; i < self.itemCount; i ++)//進行遍歷 {CGRectDivide(btnRec, &hRec, &btnRec, btnWidth, CGRectMinXEdge);NSString *title = [dataSource horizMenu:self titleForItemAtIndex:i];UIButton *menuBtn = [UIButton ButtonWithImageName:nil hImageName:nil frame:CGRectZero title:title titleColor:[UIColor blackColor]font:[UIFont boldSystemFontOfSize:15] target:self action:@selector(buttonTapped:)];// [menuBtn setBackgroundImage:self.selectedImage forState:UIControlStateSelected];[menuBtn setTitleColor:rgb(109, 209, 62) forState:UIControlStateSelected];menuBtn.tag = tag++;[self addSubview:menuBtn];if (i == 0) {//按鈕之間保持2個間距hRec = UIEdgeInsetsInsetRect(hRec, UIEdgeInsetsMake(0, 0, 0, 1));}else if (i == self.itemCount-1){hRec = UIEdgeInsetsInsetRect(hRec, UIEdgeInsetsMake(0, 1, 0, 0));}else{hRec = UIEdgeInsetsInsetRect(hRec, UIEdgeInsetsMake(0, 1, 0, 1));}menuBtn.frame = hRec;if (i != 0 && i != self.itemCount) {//在每個按鈕加 “|”hRec = CGRectMake(hRec.origin.x, hRec.origin.y,2 , hRec.size.height);hRec = UIEdgeInsetsInsetRect(hRec, UIEdgeInsetsMake(10, 0, 10, 0));UIImageView *verImgView = [UIImageView ImageViewImageName:nil frame:hRec];verImgView.image = [UIImage ImageWithColor:rgb(213, 214, 218) frame:verImgView.bounds];[self addSubview:verImgView];}}//這個在按鈕中加背景UIImageView *btnBottnImg = [UIImageView ImageViewImageName:nil frame:imgRec];btnBottnImg.image = [UIImage ImageWithColor:rgb(213, 214, 218) frame:btnBottnImg.bounds];[self addSubview:btnBottnImg];//移動背景moveImageView = [UIImageView ImageViewImageName:nil frame:UIEdgeInsetsInsetRect(imgRec, UIEdgeInsetsMake(0, 0, 0, (self.itemCount - 1) * btnWidth))];moveImageView.image = [UIImage ImageWithColor:rgb(109, 209, 62) frame:moveImageView.bounds];[self addSubview:moveImageView];}break;}[self layoutSubviews];}-(void) buttonTapped:(id) sender//按鈕點擊事件 {UIButton *button = (UIButton*) sender;for(int i = 0; i < self.itemCount; i++){UIButton *thisButton = (UIButton*) [self viewWithTag:i + kButtonBaseTag];if(i + kButtonBaseTag == button.tag)thisButton.selected = YES;elsethisButton.selected = NO;}CGRect mRec = moveImageView.frame;moveImageView.frame = CGRectMake(button.frame.origin.x,mRec.origin.y, button.frame.size.width, mRec.size.height);//移動背景 [self.itemSelectedDelegate horizMenu:self itemSelectedAtIndex:button.tag - kButtonBaseTag]; }

?

?

第二種方式 修改?MKHorizMenu.m文件

// // MKHorizMenu.m // MKHorizMenuDemo // Created by Mugunth on 09/05/11. // Copyright 2011 Steinlogic. All rights reserved. // Permission granted to do anything, commercial/non-commercial with this file apart from removing the line/URL above // Read my blog post at http://mk.sg/8h on how to use this code// As a side note on using this code, you might consider giving some credit to me by // 1) linking my website from your app's website // 2) or crediting me inside the app's credits page // 3) or a tweet mentioning @mugunthkumar // 4) A paypal donation to mugunth.kumar@gmail.com // // A note on redistribution // While I'm ok with modifications to this source code, // if you are re-publishing after editing, please retain the above copyright notices #import "MKHorizMenu.h" #define kButtonBaseTag 10000 #define kLeftOffset 10@implementation MKHorizMenu@synthesize titles = _titles; @synthesize selectedImage = _selectedImage;@synthesize itemSelectedDelegate; @synthesize dataSource; @synthesize itemCount = _itemCount;-(void) awakeFromNib {self.bounces = YES;self.scrollEnabled = YES;self.alwaysBounceHorizontal = YES;self.alwaysBounceVertical = NO;self.showsHorizontalScrollIndicator = NO;self.showsVerticalScrollIndicator = NO;[self reloadData]; }-(void) reloadData {NSArray *viewsToRemove = [self subviews];for (UIView *v in viewsToRemove) {[v removeFromSuperview];}self.itemCount = [dataSource numberOfItemsForMenu:self];self.backgroundColor = [dataSource backgroundColorForMenu:self];//self.selectedImage = [dataSource selectedItemImageForMenu:self]; 去掉背景色 UIFont *buttonFont = [UIFont boldSystemFontOfSize:15];int buttonPadding = 25;int tag = kButtonBaseTag; // int xPos = kLeftOffset;//fix 增加 自定義寬度和高度int width=self.bounds.size.width/self.itemCount;int height=self.bounds.size.height;// for(int i = 0 ; i < self.itemCount; i ++){NSString *title = [dataSource horizMenu:self titleForItemAtIndex:i];UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];[customButton setTitle:title forState:UIControlStateNormal];customButton.titleLabel.font = buttonFont;//[customButton setBackgroundImage:self.selectedImage forState:UIControlStateSelected];//去掉button的背景 customButton.tag = tag++;[customButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];int buttonWidth = [title sizeWithFont:customButton.titleLabel.fontconstrainedToSize:CGSizeMake(150, 28) lineBreakMode:UILineBreakModeClip].width;customButton.frame=CGRectMake(i*width, 0, width, height);//設置uttton通的寬度和高度 // customButton.frame = CGRectMake(xPos, 7, buttonWidth + buttonPadding, 28); // xPos += buttonWidth; // xPos += buttonPadding; [self addSubview:customButton];}// bretdabaker: added right padding to contentSize // xPos += kLeftOffset;// self.contentSize = CGSizeMake(xPos, 41); [self layoutSubviews]; }-(void) setSelectedIndex:(int) index animated:(BOOL) animated {UIButton *thisButton = (UIButton*) [self viewWithTag:index + kButtonBaseTag]; thisButton.selected = YES;[self setContentOffset:CGPointMake(thisButton.frame.origin.x - kLeftOffset, 0) animated:animated];[self.itemSelectedDelegate horizMenu:self itemSelectedAtIndex:index]; }-(void) buttonTapped:(id) sender {UIButton *button = (UIButton*) sender;for(int i = 0; i < self.itemCount; i++){UIButton *thisButton = (UIButton*) [self viewWithTag:i + kButtonBaseTag];if(i + kButtonBaseTag == button.tag)thisButton.selected = YES;elsethisButton.selected = NO;}[self.itemSelectedDelegate horizMenu:self itemSelectedAtIndex:button.tag - kButtonBaseTag]; }- (void)dealloc {[_selectedImage release];_selectedImage = nil;[_titles release];_titles = nil;[super dealloc]; }@end

分析

MKHorizMenu.m文件
#import "MKHorizMenu.h" #define kButtonBaseTag 10000 #define kLeftOffset 10@implementation MKHorizMenu@synthesize titles = _titles; @synthesize selectedImage = _selectedImage;@synthesize itemSelectedDelegate; @synthesize dataSource; @synthesize itemCount = _itemCount;-(void) awakeFromNib {self.bounces = YES;self.scrollEnabled = YES;self.alwaysBounceHorizontal = YES;self.alwaysBounceVertical = NO;self.showsHorizontalScrollIndicator = NO;self.showsVerticalScrollIndicator = NO;[self reloadData]; }-(void) reloadData {NSArray *viewsToRemove = [self subviews];for (UIView *v in viewsToRemove) {[v removeFromSuperview];}self.itemCount = [dataSource numberOfItemsForMenu:self];//得到多少按鈕self.backgroundColor = [dataSource backgroundColorForMenu:self];//得到背景色self.selectedImage = [dataSource selectedItemImageForMenu:self];//得到背景圖片 UIFont *buttonFont = [UIFont boldSystemFontOfSize:15];int buttonPadding = 25;//間距int tag = kButtonBaseTag; int xPos = kLeftOffset;for(int i = 0 ; i < self.itemCount; i ++){NSString *title = [dataSource horizMenu:self titleForItemAtIndex:i];//得到標題UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];[customButton setTitle:title forState:UIControlStateNormal];customButton.titleLabel.font = buttonFont;//設置字體 [customButton setBackgroundImage:self.selectedImage forState:UIControlStateSelected];//設置圖片 customButton.tag = tag++;//設置tag[customButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];//添加事件int buttonWidth = [title sizeWithFont:customButton.titleLabel.fontconstrainedToSize:CGSizeMake(150, 28) lineBreakMode:UILineBreakModeClip].width;customButton.frame = CGRectMake(xPos, 7, buttonWidth + buttonPadding, 28);//設置每個button的位置xPos += buttonWidth;xPos += buttonPadding;[self addSubview:customButton]; }// bretdabaker: added right padding to contentSizexPos += kLeftOffset;self.contentSize = CGSizeMake(xPos, 41); [self layoutSubviews]; }-(void) setSelectedIndex:(int) index animated:(BOOL) animated//點擊時觸發事件。 {UIButton *thisButton = (UIButton*) [self viewWithTag:index + kButtonBaseTag]; thisButton.selected = YES;[self setContentOffset:CGPointMake(thisButton.frame.origin.x - kLeftOffset, 0) animated:animated];[self.itemSelectedDelegate horizMenu:self itemSelectedAtIndex:index];//事件 }-(void) buttonTapped:(id) sender {UIButton *button = (UIButton*) sender;for(int i = 0; i < self.itemCount; i++){UIButton *thisButton = (UIButton*) [self viewWithTag:i + kButtonBaseTag];if(i + kButtonBaseTag == button.tag)thisButton.selected = YES;elsethisButton.selected = NO;}[self.itemSelectedDelegate horizMenu:self itemSelectedAtIndex:button.tag - kButtonBaseTag];//點擊事件 }- (void)dealloc {[_selectedImage release];_selectedImage = nil;[_titles release];_titles = nil;[super dealloc]; }@end

?

?

?

總結

以上是生活随笔為你收集整理的ios中MKHorizMenu用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。