iOS7风格的联系人选择器:MBContactPicker

jopen 11年前

MBContactPicker 是一个iOS7风格的联系人选择器,可作为一个UICollectionView ,实现类似 iOS 7 的本地邮件联系人选择器的功能。

#import "ViewController.h"  #import "ContactObject.h"  #import "MBContactPicker.h"    @interface ViewController () <MBContactPickerDataSource, MBContactPickerDelegate>    @property (nonatomic) NSArray *contacts;  @property (weak, nonatomic) IBOutlet MBContactPicker *contactPickerView;  @property (weak, nonatomic) IBOutlet NSLayoutConstraint *contactPickerViewHeightConstraint;    @end    @implementation ViewController    - (void)viewDidLoad  {      [super viewDidLoad];        NSArray *array = @[                         @{@"Name":@"Contact 1", @"Title":@"CTO"},                         @{@"Name":@"Contact 2", @"Title":@"CEO"}                         ];        NSMutableArray *contacts = [[NSMutableArray alloc] initWithCapacity:array.count];      for (NSDictionary *contact in array)      {          ContactObject *model = [[ContactObject alloc] init];          model.contactTitle = contact[@"Name"];          model.contactSubtitle = contact[@"Title"];          [contacts addObject:model];      }      self.contacts = contacts;        self.contactPickerView.delegate = self;      self.contactPickerView.datasource = self;  }    #pragma mark - MBContactPickerDataSource    // Use this method to give the contact picker the entire set of possible contacts.  Required.  - (NSArray *)contactModelsForContactPicker:(MBContactPicker*)contactPickerView  {      return self.contacts;  }    // Use this method to pre-populate contacts in the picker view.  Optional.  - (NSArray *)selectedContactModelsForContactPicker:(MBContactPicker*)contactPickerView  {      return @[];  }    #pragma mark - MBContactPickerDelegate    // Optional  - (void)contactCollectionView:(MBContactCollectionView*)contactCollectionView didSelectContact:(id<MBContactPickerModelProtocol>)model  {      NSLog(@"Did Select: %@", model.contactTitle);  }    // Optional  - (void)contactCollectionView:(MBContactCollectionView*)contactCollectionView didAddContact:(id<MBContactPickerModelProtocol>)model  {      NSLog(@"Did Add: %@", model.contactTitle);  }    // Optional  - (void)contactCollectionView:(MBContactCollectionView*)contactCollectionView didRemoveContact:(id<MBContactPickerModelProtocol>)model  {      NSLog(@"Did Remove: %@", model.contactTitle);  }    // Optional  // This delegate method is called to allow the parent view to increase the size of  // the contact picker view to show the search table view  - (void)didShowFilteredContactsForContactPicker:(MBContactPicker*)contactPicker  {      if (self.contactPickerViewHeightConstraint.constant <= contactPicker.currentContentHeight)      {          [UIView animateWithDuration:contactPicker.animationSpeed animations:^{              CGRect pickerRectInWindow = [self.view convertRect:contactPicker.frame fromView:nil];              CGFloat newHeight = self.view.window.bounds.size.height - pickerRectInWindow.origin.y - contactPicker.keyboardHeight;              self.contactPickerViewHeightConstraint.constant = newHeight;              [self.view layoutIfNeeded];          }];      }  }    // Optional  // This delegate method is called to allow the parent view to decrease the size of  // the contact picker view to hide the search table view  - (void)didHideFilteredContactsForContactPicker:(MBContactPicker*)contactPicker  {      if (self.contactPickerViewHeightConstraint.constant > contactPicker.currentContentHeight)      {          [UIView animateWithDuration:contactPicker.animationSpeed animations:^{              self.contactPickerViewHeightConstraint.constant = contactPicker.currentContentHeight;              [self.view layoutIfNeeded];          }];      }  }    // Optional  // This delegate method is invoked to allow the parent to increase the size of the  // collectionview that shows which contacts have been selected. To increase or decrease  // the number of rows visible, change the maxVisibleRows property of the MBContactPicker  - (void)contactPicker:(MBContactPicker*)contactPicker didUpdateContentHeightTo:(CGFloat)newHeight  {      self.contactPickerViewHeightConstraint.constant = newHeight;      [UIView animateWithDuration:contactPicker.animationSpeed animations:^{          [self.view layoutIfNeeded];      }];  }    @end

iOS7风格的联系人选择器:MBContactPicker

项目主页:http://www.open-open.com/lib/view/home/1388915798609