2012年11月10日 星期六

Apple TV的遙控器

最近使用上發現

Apple TV的遙控器可以控制Mac 上的iTunes播放,有時候會覺得有些困擾,但如果要看iTunes就覺得還不錯。

遙控器可以控制
播放/暫停,大小聲,前後影音檔。



不過不喜歡的話還是可以關掉的

此處可以參考
http://www.macuknow.com/node/16683


以下是轉帖的內容

解決方式有兩個:
1.配對Apple TV與Apple遙控器
配對之後,這個Apple遙控器就只能控制Apple TV,不能控制Mac電腦了~
請進入Apple TV的『設定>>一般>>遙控器>>配對Apple Remote遙控器』

 

2.關閉Mac電腦的紅外線接受器
請進入『系統偏好設定>>安全性與隱私>>一般>>停用遠端控制紅外線接收器』

Slider的基本使用(二)

延續Slider的基本使用(ㄧ)的設計,加上圖示,改變外觀

1. 先在專案上加入圖檔,這邊使用mac內置的圖檔

2. 在SliderViewController.m加上圖檔的設定
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    appDelegate = (mainAppDelegate *)[[UIApplication sharedApplication] delegate];
   
    mySlider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
    [mySlider addTarget:self action:@selector(controlValueChanged) forControlEvents:UIControlEventValueChanged];
   
    mySlider.minimumValue = 0;
    mySlider.maximumValue = 3;
    mySlider.continuous = YES;
   
    NSString *imageName1 = [NSString stringWithFormat:@"Doggie.gif"];
   
    NSString *imageName2 = [NSString stringWithFormat:@"Pointy.gif"];
   
    NSString *imageName3 = [NSString stringWithFormat:@"RedDog.gif"];
   
   
    UIImage *minImage = [[UIImage imageNamed:imageName1] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
   
    UIImage *maxImage = [[UIImage imageNamed:imageName2] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
 

//UIImage提供了resizableImageWithCapInsets方法,可以对图案进行拉伸处理。参数//UIEdgeInsetsMake(0,0,0,0)指定了不进行拉伸操作的部分的大小,顺序是上、左、下、右4个 
边。   

    UIImage *thumbImage = [UIImage imageNamed:imageName3];

   
    [mySlider setMaximumTrackImage:maxImage forState:UIControlStateNormal];
    [mySlider setMinimumTrackImage:minImage forState:UIControlStateNormal];
   
    [mySlider  setThumbImage:thumbImage  forState:UIControlStateNormal];

   
   
    // voiceSlider.value = 0.5;  // 此種設定會讓Slider不能動
    [mySlider setValue:lastvalue]; //只可以用此法,
   
    [self.view addSubview:mySlider];
   
 }

3. 執行結果

2012年11月9日 星期五

文字陰影的特殊效果

Xcode在IOS5之後,提供一個特殊功能來顯示文字陰影。這個功能可以在有文字顯示需求的地方來使用,例如SegmentController/NavigationBar/TabBar  之類的,只要有setTitleTextAttributes這個function可以呼叫來加入屬性,就可以使用。實驗顯示的效果並不美觀,不知在何種應用需要這個功能。

官方文件
http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html


以下的實驗例,改自UISegmentedControl的使用(三)。

1. 加入兩個Slider到storyboard,並外加兩個Label來註解。

2. 在mainViewController.h加入Slider的註冊,並外加Toolbar及BarItem的元件。
#import <UIKit/UIKit.h>

@interface mainViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentSelector;

@property (nonatomic) UISegmentedControl *selector1;

@property (nonatomic) UISegmentedControl *selector2;

@property (nonatomic) UISegmentedControl *selector3;

@property (weak, nonatomic) IBOutlet UISlider *shadowXSlider;

@property (weak, nonatomic) IBOutlet UISlider *shadowYSlider;


@property (nonatomic) UIToolbar *myToolBar;

@property (nonatomic) UIBarButtonItem *myBarItem1;
@property (nonatomic) UIBarButtonItem *myBarItem2;
@property (nonatomic) UIBarButtonItem *myBarItem3;
@property (nonatomic) UIBarButtonItem *myBarItem4;
@property (nonatomic) UIBarButtonItem *myBarItem5;


@end

3.在mainViewController.m加入相關程式碼
#import "mainViewController.h"

@interface mainViewController ()

@end

@implementation mainViewController

@synthesize segmentSelector;
@synthesize selector1, selector2, selector3;

@synthesize shadowXSlider, shadowYSlider;

@synthesize myToolBar;

@synthesize myBarItem1, myBarItem2, myBarItem3, myBarItem4, myBarItem5;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
   
    [self setupMyToolBar];
   
    [self.view  addSubview:myToolBar];

   
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)selectOtherSegment:(id)sender {
   
    NSNumber *value = [NSNumber numberWithInt:[sender selectedSegmentIndex]];
   
    switch ([sender selectedSegmentIndex]) {
        case 0:
           
            [self setSelector1];
           
            break;
           
        case 1:
           
            [self setSelector2];
           
            break;
           
        case 2:
           
            [self setSelector3];
           
            break;
           
        default:
           
            [self setSelector1];
           
            break;
    }

}


- (void) setSelector1
{
    [self removeSubViewByLabelClass];
   
    NSArray *itemArray =[NSArray arrayWithObjects:@"Green Apple", @"Orange", @"Pear",@"Red Apple", @"Strawberry",nil];
   
    //使用陣列來建立UISegmentedControl
    selector1 = [[UISegmentedControl alloc] initWithItems:itemArray];
   
    //設定外觀大小與初始選項
    selector1.segmentedControlStyle = UISegmentedControlStyleBar;
    selector1.frame = CGRectMake(20.0, 100.0, 660.0, 44.0);
    selector1.selectedSegmentIndex = 0;
   
   
     UIImage *image1 = [UIImage imageNamed:@"Orange.gif"];
   
    //[[selector1 appearance] setBackgroundImage:image1 forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
   
    [selector1 setBackgroundImage:image1 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
   
    UIImage *segmentedControl = [UIImage imageNamed:@"pear.gif"];
      
   [selector1 setDividerImage:segmentedControl forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
   
   
    UIFont *font = [UIFont boldSystemFontOfSize:16];//改變segment的字體大小
    NSDictionary *attribute = [NSDictionary dictionaryWithObject:font forKey:UITextAttributeFont];
    [selector1 setTitleTextAttributes:attribute forState:UIControlStateNormal];
    //selector1.momentary = YES; // 不顯示被選的狀態
   
    
    selector1.tag = 1;
   
    //設定所觸發的事件條件與對應事件
    [selector1 addTarget:self action:@selector(chooseOne:) forControlEvents:UIControlEventValueChanged];
   
    //加入畫面中並釋放記憶體
    [self.view addSubview:selector1];
   
}

- (void) setSelector2
{
    [self removeSubViewByLabelClass];
   
    NSArray *itemArray =[NSArray arrayWithObjects:@"Bluehound", @"Doggie", @"Pointy",@"PurpGuy",@"RedDog",@"Woof", nil];
   
    //使用陣列來建立UISegmentedControl
    selector2 = [[UISegmentedControl alloc] initWithItems:itemArray];
   
    //設定外觀大小與初始選項
    selector2.segmentedControlStyle = UISegmentedControlStyleBar;
    selector2.frame = CGRectMake(20.0, 200.0, 600.0, 44.0);
    selector2.selectedSegmentIndex = 0;
   
    selector2.tag = 2;
   
    //[UIColor clearColor], UITextAttributeTextShadowColor,
   
//    [selector2 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
//                                       [UIColor magentaColor],UITextAttributeTextColor,
//                                       [UIColor blackColor], UITextAttributeTextShadowColor,
//                                       [NSValue valueWithUIOffset:UIOffsetMake(0, -5)], UITextAttributeTextShadowOffset,
//                                       [UIFont fontWithName:@"Courier-Oblique" size:22.0], UITextAttributeFont, nil] forState:UIControlStateNormal];
//   
   
    //UIFont *font = [UIFont boldSystemFontOfSize:16];//改變segment的字體大小
   
    NSDictionary *attribute = [NSDictionary dictionaryWithObjectsAndKeys:
                    [UIColor magentaColor],                             UITextAttributeTextColor,
                    [UIColor blackColor],                               UITextAttributeTextShadowColor,
                    [NSValue valueWithUIOffset:UIOffsetMake(shadowXSlider.value, shadowYSlider.value)],    UITextAttributeTextShadowOffset,
                    [UIFont fontWithName:@"Courier-Oblique" size:22.0], UITextAttributeFont,
                               nil];
   
    [selector2 setTitleTextAttributes:attribute forState:UIControlStateNormal];
   

   
    //設定所觸發的事件條件與對應事件
    [selector2 addTarget:self action:@selector(chooseOne:) forControlEvents:UIControlEventValueChanged];
   
    //加入畫面中並釋放記憶體
    [self.view addSubview:selector2];
   
}

- (void) setSelector3
{
    [self removeSubViewByLabelClass];
   
    NSArray *itemArray =[NSArray arrayWithObjects:@"African Daisy", @"Dandelion", @"Ixia", @"Spiked",@"Sunflower",nil];
   
    //使用陣列來建立UISegmentedControl
    selector3 = [[UISegmentedControl alloc] initWithItems:itemArray];
   
    //設定外觀大小與初始選項
    selector3.segmentedControlStyle = UISegmentedControlStylePlain; //UISegmentedControlStyleBar;
   
    selector3.frame = CGRectMake(20.0, 300.0, 550.0, 44.0);
    selector3.selectedSegmentIndex = 0;
   
    selector3.tag = 3;
   
    //設定所觸發的事件條件與對應事件
    [selector3 addTarget:self action:@selector(chooseOne:) forControlEvents:UIControlEventValueChanged];
   
    //加入畫面中並釋放記憶體
    [self.view addSubview:selector3];
   
}

- (void)chooseOne:(id)sender {
   
    [self removeSubViewByTag:101];
   
    //NSLog(@"%@", [sender titleForSegmentAtIndex:[sender selectedSegmentIndex]]);
   
    UIImageView *a1 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 500, 200,200)];
    [a1 setImage: [UIImage imageNamed:[NSString stringWithFormat:@"%@.gif",[sender titleForSegmentAtIndex:[sender selectedSegmentIndex]]]]];
   
    //a1.alpha = 0.3f;
   
    a1.tag = 101;
   
    [self.view addSubview:a1];
    [self.view sendSubviewToBack:a1];
   
   
}

- (void) removeSubViewByTag:(int)tag
{
    for(UIView *subview in [self.view subviews]) {
        if (subview.tag == tag)
            [subview removeFromSuperview];
    }
}

- (void) removeSubViewByLabelClass
{
    for(UIView *subview in [self.view subviews]) {
        if([subview isKindOfClass:[UISegmentedControl class]]) {
           
            if (subview.tag != 100)
                [subview removeFromSuperview];
        } else {
            // Do nothing - not a UIButton or subclass instance
        }
    }
}

-(void)setupMyToolBar
{
    myToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 684, 1024, 64.0)];
   
   
    NSDictionary *attribute1 = [NSDictionary dictionaryWithObjectsAndKeys:
                               [UIColor magentaColor],                             UITextAttributeTextColor,
                               [UIColor blackColor],                               UITextAttributeTextShadowColor,
                               [NSValue valueWithUIOffset:UIOffsetMake(-5, 5)],    UITextAttributeTextShadowOffset,
                               [UIFont fontWithName:@"Courier-Oblique" size:16.0], UITextAttributeFont,
                               nil];
   
    NSDictionary *attribute2 = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIColor magentaColor],                             UITextAttributeTextColor,
                                [UIColor blueColor],                               UITextAttributeTextShadowColor,
                                [NSValue valueWithUIOffset:UIOffsetMake(10, 10)],    UITextAttributeTextShadowOffset,
                                [UIFont fontWithName:@"Courier-Oblique" size:22.0], UITextAttributeFont,
                                nil];
   
    NSDictionary *attribute3 = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIColor redColor],                             UITextAttributeTextColor,
                                [UIColor blackColor],                               UITextAttributeTextShadowColor,
                                [NSValue valueWithUIOffset:UIOffsetMake(-10, -10)],    UITextAttributeTextShadowOffset,
                                [UIFont fontWithName:@"Courier-Oblique" size:22.0], UITextAttributeFont,
                                nil];
   
    NSDictionary *attribute4 = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIColor magentaColor],                             UITextAttributeTextColor,
                                [UIColor greenColor],                               UITextAttributeTextShadowColor,
                                [NSValue valueWithUIOffset:UIOffsetMake(-15, 15)],    UITextAttributeTextShadowOffset,
                                [UIFont fontWithName:@"Courier" size:30.0], UITextAttributeFont,
                                nil];
   
    NSDictionary *attribute5 = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIColor blueColor],                             UITextAttributeTextColor,
                                [UIColor greenColor],                               UITextAttributeTextShadowColor,
                                [NSValue valueWithUIOffset:UIOffsetMake(10, 10)],    UITextAttributeTextShadowOffset,
                                [UIFont fontWithName:@"Courier" size:30.0], UITextAttributeFont,
                                nil];
   
   
    //myBarItem1= [[UIBarButtonItem alloc] initWithCustomView:label];
    myBarItem1= [[UIBarButtonItem alloc]initWithTitle:@"Test1" style:UIBarButtonItemStylePlain target:self action:@selector(showText1)];
   
    myBarItem2= [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFixedSpace) target:nil action:nil];
   
   
    myBarItem3= [[UIBarButtonItem alloc]initWithTitle:@"Test2" style:UIBarButtonItemStyleBordered target:self action:@selector(showText2)];
   
    myBarItem4= [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:nil action:nil];
   
    myBarItem5= [[UIBarButtonItem alloc]initWithTitle:@"Test3" style:UIBarButtonItemStyleDone target:self action:@selector(showText3)];
   
   
    [myBarItem1 setTitleTextAttributes:attribute1 forState:UIControlStateNormal];
    [myBarItem2 setTitleTextAttributes:attribute2 forState:UIControlStateNormal];
    [myBarItem3 setTitleTextAttributes:attribute3 forState:UIControlStateNormal];
    [myBarItem4 setTitleTextAttributes:attribute4 forState:UIControlStateNormal];
    [myBarItem5 setTitleTextAttributes:attribute5 forState:UIControlStateNormal];

     myBarItem2.width = 420;
   
    NSArray *toolbarItems = [[NSArray alloc] initWithObjects:myBarItem1,myBarItem2,myBarItem3,myBarItem4,myBarItem5,nil];
    myToolBar.items = toolbarItems;
   
}


@end

4. 顯示結果,Toolbar上每個Item的字型及陰影都不同,而Segment2的陰影則可由兩個Slider來控制,只是不知道什麼樣的應用需要而已。

2012年11月8日 星期四

UISegmentedControl的使用(三)


接續前一篇UISegmentedControl的使用(二)

有兩個設定可以稍稍改變UISegmentedControl的外觀

直接實作,從前一篇修改

- (void) setSelector1
{
    [self removeSubViewByLabelClass];
   
    NSArray *itemArray =[NSArray arrayWithObjects:@"Green Apple", @"Orange", @"Pear",@"Red Apple", @"Strawberry",nil];
   
    //使用陣列來建立UISegmentedControl
    selector1 = [[UISegmentedControl alloc] initWithItems:itemArray];
   
    //設定外觀大小與初始選項
    selector1.segmentedControlStyle = UISegmentedControlStyleBar;
    selector1.frame = CGRectMake(20.0, 100.0, 660.0, 44.0);
    selector1.selectedSegmentIndex = 0;
   
   
     UIImage *image1 = [UIImage imageNamed:@"Orange.gif"];
   
    [selector1 setBackgroundImage:image1 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
   
    UIImage *image2 = [UIImage imageNamed:@"pear.gif"];
      
   [selector1 setDividerImage:image2 forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

     
    UIFont *font = [UIFont boldSystemFontOfSize:16];//改變segment的字體大小
    NSDictionary *attribute = [NSDictionary dictionaryWithObject:font forKey:UITextAttributeFont];
    [selector1 setTitleTextAttributes:attribute forState:UIControlStateNormal];
    //selector1.momentary = YES; // 不顯示被選的狀態
   
    selector1.tag = 1;
   
    //設定所觸發的事件條件與對應事件
    [selector1 addTarget:self action:@selector(chooseOne:) forControlEvents:UIControlEventValueChanged];
   
    //加入畫面中並釋放記憶體
    [self.view addSubview:selector1];
   
}


結果不太理想,顯示的效果滿醜的,使用Pear來分割,Orange來做背景。
還不如自己用button的組合來做,我想應該不太有人想要用這個做法。


2012年11月7日 星期三

如何加入一個admob廣告到App

接前一篇的Admob的註冊。

註冊好了,當然要將Admob加到App上,做法可參考Google的文件,原文寫得較完整。

官方原文參考作法  https://developers.google.com/mobile-ads-sdk/docs/
官方中文參考做法  https://developers.google.com/mobile-ads-sdk/docs/ios/fundamentals?hl=zh-TW



以下是我的實驗程序。

1. 開一個實驗專案

2. 將googleadmobadssdkios.zip解壓縮後

3. 將.h及.a檔,加到專案裡



   
 
4. 然後要加上這個SDK所需要的framework,列表如下
  • StoreKit
  • AudioToolbox
  • MessageUI
  • SystemConfiguration
  • CoreGraphics
  • AdSupport

5.在storyboard上加上測試字樣,以示區別

6. 在mainViewController.h加上Admob元件

#import <UIKit/UIKit.h>
#import "GADBannerView.h"

@interface mainViewController : UIViewController

@property (nonatomic) GADBannerView *bannerView_;

@end

7.在編輯mainViewController.m之前,要先找出發佈商ID(AdMob Publisher ID),在Admob的網站上登入後,在網站與應用程式上找,參考截圖,此例的ID採用另一個App的ID來做測試。



8. 找到ID後,依照參考方法在mainViewController.m加入admob程式碼
#import "mainViewController.h"

@interface mainViewController ()

@end

@implementation mainViewController

@synthesize bannerView_;

#define MY_BANNER_UNIT_ID  @"a1xxxxxxxxxx"
  <- 加入App的對應ID


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // 在畫面下方建立標準廣告大小的畫面。
    bannerView_ = [[GADBannerView alloc]
                   initWithFrame:CGRectMake(0.0,680.0, 960.0, 120.0)];
   
    // 指定廣告的「單元識別碼」,也就是您的 AdMob 發佈商編號。
    bannerView_.adUnitID = MY_BANNER_UNIT_ID;
   
    // 指定要復原的 UIViewController,讓執行階段在每次擷取
    // 點擊廣告的使用者後加以復原,並加進檢視階層。
    bannerView_.rootViewController = self;
    [self.view addSubview:bannerView_];
   
    // 啟用泛用請求,並隨廣告一起載入。
    [bannerView_ loadRequest:[GADRequest request]];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

9. 還要加個Linking參數-ObjC到專案內,請參考圖上位置。


10. 執行後的結果如下,廣告已經出現在畫面的下方了。

11. 這兩天的測試,居然已經有一些廣告收入,只是測試時點了幾下,真不錯。


2012年11月6日 星期二

admob帳號申請

在App的廣告中主要有iAD及Admob兩個,本來想說第一個廣告App就先用Apple的iAD。但是Apple的廣告禁止在小孩專用的App上使用,因此只好先用Admob了

詳請可參考
https://developer.apple.com/cn/support/ios/iad-network.html

如何加入 iAd Network?

您必须注册 iOS Developer Program才可以加入 iAd Network。会员需要执行以下操作:
  • 同意 iTunes Connect 中的开发者广告服务协议。
  • 如果您尚未在 App Store 发布收费应用程序,则请先设置您的银行和税务信息。
  • 在 iTunes Connect 的“Manage Your Applications”模块中,让应用程序支持 iAd 广告。
  • 在 iTunes Connect 的“iAd Network”模块中设置 iAd 的偏好信息。
请注意:Apple 政策禁止在适用于儿童的应用程序中添加 iAd 广告。

開始Admob的帳號申請

也可以參考http://www.minwt.com/ios/4511.html
說明很仔細。

以下是我自己的實作記錄

1. 註冊Admob賬號

要使用Admob,就要先申請帳號,因為本來就有google mail的帳號,因此就直接使用了(如圖二的右邊選項)。




 2.登記個人資料


3. App註冊到Admob,剛註冊完就會有圖一的App註冊選項。如果以後才要註冊App,就從Admob首頁(如圖二)。

3. 中間會出現需要註冊銀行帳號資料,或是自己先去註冊銀行帳號,資料都要是英文的。
可參考網頁: http://wazai.net/2256/google-admob%E9%80%8F%E9%81%8Ehsbc-direct%E9%9B%BB%E5%8C%AF%E6%96%B9%E5%BC%8F%E8%AB%8B%E6%AC%BE

其中銀行的SWIFT Code參考下面網頁,不過最好還是打電話問一下銀行比較方便及安全,銀行會將相關資料傳真給你。
台灣地區銀行匯款代碼SWIFT CODE查詢總列表:

http://www.swiftcode.info/bank/?BankCountryEN=TAIWAN





4. 將App的資料填寫到網頁上,我是直接將iTunes Connect上的App資料貼過來。

5. 註冊完App的資料後,就會出現SDK的下載,下載後再將程式碼加入到App內部吧







2012年11月5日 星期一

UISegmentedControl的使用(二)

在動態使用Segment時,有需要修改其字型的使用,使得顯現的效果較為美觀。

為了要將字型的設定套到Segment中,要借助NSDictionary來設定屬性。

方法可參考
http://furnacedigital.blogspot.tw/2011/11/appearance.html



實作參考例

- (void) setSelector1
{
    [self removeSubViewByLabelClass];
   
    NSArray *itemArray =[NSArray arrayWithObjects:@"Green Apple", @"Orange", @"Pear",@"Red Apple", @"Strawberry",nil];
   
    //使用陣列來建立UISegmentedControl
    selector1 = [[UISegmentedControl alloc] initWithItems:itemArray];
   
    //設定外觀大小與初始選項
    selector1.segmentedControlStyle = UISegmentedControlStyleBar;
    selector1.frame = CGRectMake(20.0, 100.0, 500.0, 44.0);
    selector1.selectedSegmentIndex = 0;
   
    UIFont *font = [UIFont boldSystemFontOfSize:16];//改變segment的字體大小
    NSDictionary *attribute = [NSDictionary dictionaryWithObject:font forKey:UITextAttributeFont];
    [selector1 setTitleTextAttributes:attribute forState:UIControlStateNormal];
    


    selector1.momentary = YES; // 不顯示被選的狀態,特殊效果可用可不用
   
    selector1.tag = 1;
   
    //設定所觸發的事件條件與對應事件
    [selector1 addTarget:self action:@selector(chooseOne:) forControlEvents:UIControlEventValueChanged];
   
    //加入畫面中並釋放記憶體
    [self.view addSubview:selector1];
   
}


執行結果
有改變字型,左邊的Segment

使用系統內定

2012年11月4日 星期日

UISegmentedControl的使用(一)

UISegmentedControl方便使用者來做選擇,

參考網頁 http://furnacedigital.blogspot.tw/2011/08/uisegmentedcontrol.html

 UISegmentedControl 可以製作出多選項的按鈕物件,有點類似 Radio Button 的感覺,使用者在同一組選項中,一次只能選擇一個項目,每一個項目的背後都會有一個對應值,它們可以是該項目的 Title 、Index 或是自行設定的 Array 參數。

實作上有靜態及動態設定,因此在實作上使用一個靜態來選擇三個動態的UISegmentedControl元件,並以動態的UISegmentedControl元件來選擇圖檔。

以下是我的實作例

1. 開啓一個專案

2. 拉入一個UISegmentedControl元件到storyBoard,並設定Tag為 100

3. 加入圖檔到專案內,此處使用Mac上的圖檔

4. 將UISegmentedControl元件註冊到mainViewController.h,並加入三個UISegmentedControl給動態設定使用。

#import <UIKit/UIKit.h>

@interface mainViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentSelector;

@property (nonatomic) UISegmentedControl *selector1;

@property (nonatomic) UISegmentedControl *selector2;

@property (nonatomic) UISegmentedControl *selector3;


@end

5. 將相關的程式碼加到mainViewController.m
#import "mainViewController.h"

@interface mainViewController ()

@end

@implementation mainViewController

@synthesize segmentSelector;
@synthesize selector1, selector2, selector3;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)selectOtherSegment:(id)sender {
   
    NSNumber *value = [NSNumber numberWithInt:[sender selectedSegmentIndex]];
   
    switch ([sender selectedSegmentIndex]) {
        case 0:
           
            [self setSelector1];
           
            break;
           
        case 1:
           
            [self setSelector2];
           
            break;
           
        case 2:
           
            [self setSelector3];
           
            break;
           
        default:
           
            [self setSelector1];
           
            break;
    }

}


- (void) setSelector1
{
    [self removeSubViewByLabelClass];
   
    NSArray *itemArray =[NSArray arrayWithObjects:@"Green Apple", @"Orange", @"Pear",@"Red Apple", @"Strawberry",nil];
   
    //使用陣列來建立UISegmentedControl
    selector1 = [[UISegmentedControl alloc] initWithItems:itemArray];
   
    //設定外觀大小與初始選項
    selector1.segmentedControlStyle = UISegmentedControlStyleBar;
    selector1.frame = CGRectMake(20.0, 100.0, 500.0, 44.0);
    selector1.selectedSegmentIndex = 0;
   
    selector1.tag = 1;
   
    //設定所觸發的事件條件與對應事件
    [selector1 addTarget:self action:@selector(chooseOne:) forControlEvents:UIControlEventValueChanged];
   
    //加入畫面中並釋放記憶體
    [self.view addSubview:selector1];
   
}

- (void) setSelector2
{
    [self removeSubViewByLabelClass];
   
    NSArray *itemArray =[NSArray arrayWithObjects:@"Bluehound", @"Doggie", @"Pointy",@"PurpGuy",@"RedDog",@"Woof", nil];
   
    //使用陣列來建立UISegmentedControl
    selector2 = [[UISegmentedControl alloc] initWithItems:itemArray];
   
    //設定外觀大小與初始選項
    selector2.segmentedControlStyle = UISegmentedControlStyleBar;
    selector2.frame = CGRectMake(20.0, 200.0, 600.0, 44.0);
    selector2.selectedSegmentIndex = 0;
   
    selector1.tag = 2;
   
    //設定所觸發的事件條件與對應事件
    [selector2 addTarget:self action:@selector(chooseOne:) forControlEvents:UIControlEventValueChanged];
   
    //加入畫面中並釋放記憶體
    [self.view addSubview:selector2];
   
}

- (void) setSelector3
{
    [self removeSubViewByLabelClass];
   
    NSArray *itemArray =[NSArray arrayWithObjects:@"African Daisy", @"Dandelion", @"Ixia", @"Spiked",@"Sunflower",nil];
   
    //使用陣列來建立UISegmentedControl
    selector3 = [[UISegmentedControl alloc] initWithItems:itemArray];
   
    //設定外觀大小與初始選項
    selector3.segmentedControlStyle = UISegmentedControlStylePlain; //UISegmentedControlStyleBar;
   
    selector3.frame = CGRectMake(20.0, 300.0, 550.0, 44.0);
    selector3.selectedSegmentIndex = 0;
   
    selector1.tag = 3;
   
    //設定所觸發的事件條件與對應事件
    [selector3 addTarget:self action:@selector(chooseOne:) forControlEvents:UIControlEventValueChanged];
   
    //加入畫面中並釋放記憶體
    [self.view addSubview:selector3];
   
}

- (void)chooseOne:(id)sender {
   
    [self removeSubViewByTag:101];
   
    //NSLog(@"%@", [sender titleForSegmentAtIndex:[sender selectedSegmentIndex]]);
   
    UIImageView *a1 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 500, 200,200)];
    [a1 setImage: [UIImage imageNamed:[NSString stringWithFormat:@"%@.gif",[sender titleForSegmentAtIndex:[sender selectedSegmentIndex]]]]];
   
    //a1.alpha = 0.3f;
   
    a1.tag = 101;
   
    [self.view addSubview:a1];
    [self.view sendSubviewToBack:a1];
   
   
}

- (void) removeSubViewByTag:(int)tag
{
    for(UIView *subview in [self.view subviews]) {
        if (subview.tag == tag)
            [subview removeFromSuperview];
    }
}

- (void) removeSubViewByLabelClass
{
    for(UIView *subview in [self.view subviews]) {
        if([subview isKindOfClass:[UISegmentedControl class]]) {
           
            if (subview.tag != 100)
                [subview removeFromSuperview];
        } else {
            // Do nothing - not a UIButton or subclass instance
        }
    }
}


@end

6. 執行結果,選擇右邊的UISegmentedControl,來產生左邊的UISegmentedControl,再以左邊的選擇來展示同名的圖檔。