標籤:ble forms enter alignment png lex gen present uid
終於:
程式碼片段:
//// DockItemLocation.m// 帥哥_團購//// Created by beyond on 14-8-13.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "DockItemLocation.h"// 點擊dock上面的locationBtn,彈出的Popover封裝的控制器,其上方是搜尋欄,下方是tableView#import "CityLocationController.h"// button上面是圖片,以下是文字,這是圖片在高度上的比例#define kImageHeightRatioInBtn 0.5@interface DockItemLocation()<UIPopoverControllerDelegate>{ //popover控制器,建立出來之後,show方法顯示,因此不能夠是局部變數,必須用成員變數記住,否則方法btnClick調用完成就銷毀了,還怎樣 顯示捏? UIPopoverController *_popoverCtrl;}@end@implementation DockItemLocation- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // 1.調用父類的方法,設定內部的圖片 [self setIcon:@"ic_district.png" selectedIcon:@"ic_district_hl.png"]; // 2.自己主動伸縮 self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; // 3.設定預設的文字 [self setTitle:@"定位中" forState:UIControlStateNormal]; self.titleLabel.font = [UIFont systemFontOfSize:16]; self.titleLabel.textAlignment = NSTextAlignmentCenter; [self setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled]; [self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; // 4.設定圖片屬性 self.imageView.contentMode = UIViewContentModeCenter; // 5.監聽點擊【Location定位】,彈出一個Popover控制器 [self addTarget:self action:@selector(locationBtnOnDockClicked) forControlEvents:UIControlEventTouchDown]; } return self;}// 5.監聽點擊【Location定位】,彈出一個Popover控制器- (void)locationBtnOnDockClicked{ // 禁用,僅僅可點擊一次 self.enabled = NO; // 點擊dock上面的locationBtn,彈出的Popover封裝的控制器,其上方是搜尋欄,下方是tableView CityLocationController *cityVC = [[CityLocationController alloc] init]; // 唯一一個不是繼承自UIViewController的控制器,它繼承自NSObject //popover控制器,建立出來之後,show方法顯示,因此不能夠是局部變數,必須用成員變數記住,否則方法btnClick調用完成就銷毀了,還怎樣 顯示捏? _popoverCtrl = [[UIPopoverController alloc] initWithContentViewController:cityVC]; // 設定這個Popover控制器的顯示的大小 _popoverCtrl.popoverContentSize = CGSizeMake(320, 480); // 代理,監聽Popover控制器的XX事件 _popoverCtrl.delegate = self; // 由於其它方法也要顯示,_popoverCtrl,所以抽取成自己定義方法 [self showPopoverCtrl]; // 由於旋轉螢幕時,彈出的popover的指向的位置就不正確了,所以有必要注冊監聽旋轉螢幕的通知 // 先移除監聽器,保證健壯性 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; // 再加入一個監聽器,一旦裝置出現UIDeviceOrientationDidChangeNotification,就會調用observer的selector方法 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenDidRotated) name:UIDeviceOrientationDidChangeNotification object:nil];}// 5-1,由於偵聽到旋轉螢幕了,也要再次顯示_popoverCtrl,所以抽取成自己定義方法- (void)showPopoverCtrl{ // 顯示到哪裡?
假設目標view是self自己,則rect使用bounds,由於bounds的原點才是相對於自己 // 假設目標view是self.superView,則rect使用frame,由於frame的原點才是相對於父控制項 [_popoverCtrl presentPopoverFromRect:self.bounds inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];}// 5-2,再加入一個監聽器,一旦裝置出現UIDeviceOrientationDidChangeNotification,就會調用observer的selector方法- (void)screenDidRotated{ if (_popoverCtrl.popoverVisible) { // 1. 關閉之前位置上面的_popoverCtrl [_popoverCtrl dismissPopoverAnimated:NO]; // 2. 0.5秒後建立新的位置上的_popoverCtrl [self performSelector:@selector(showPopoverCtrl) withObject:nil afterDelay:0.5]; }}#pragma mark - popOver代理方法- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{ // 消失前,讓定位button恢複能夠點擊狀態 self.enabled = YES; // 消失前,即popover被銷毀的時候。移除注冊的監聽器(通知) [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];}#pragma mark - 銷毀時,移除當前對控制器對螢幕的監聽,防止野指標- (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self];}#pragma mark - 覆寫調整圖片和文字在button中的Frame- (CGRect)imageRectForContentRect:(CGRect)contentRect{ CGFloat btnW = contentRect.size.width; CGFloat imgH = contentRect.size.height * kImageHeightRatioInBtn; return CGRectMake(0, 0, btnW, imgH);}- (CGRect)titleRectForContentRect:(CGRect)contentRect{ CGFloat btnW = contentRect.size.width; CGFloat textH = contentRect.size.height * (1 - kImageHeightRatioInBtn); // 文字在以下,圖片在上面 CGFloat textY = contentRect.size.height - textH; return CGRectMake(0, textY, btnW, textH);}@end
iOS_21團購_Popover適應iPad橫豎屏切換