2012年4月19日 星期四

Filter4Cam 實作: 16. 調整濾鏡表單位置之二

since: 2012/04/19
update: 2012/04/20


A. 調整濾鏡表單水平擺放的位置

      1. 開啓 ViewController.m 檔案, 修改如下:
....
- (UITableView *)filterListTableView
{  
    if (_filterListTableView == nil) {
        ....
        //@update: Tune UI
        /*
        _filterListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kOverlayTableViewRowHeight) style:UITableViewStyleGrouped];
        */
        //@update: Tune for Orientation
        _filterListTableView = [[UITableView alloc] init];
    }
   
    return _filterListTableView;
}
....
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{   
    //@update
    // 設備垂直擺放
    if (interfaceOrientation == UIDeviceOrientationPortrait)
    {
        //@update: Tune UI
        [self.filterLensImageView setFrame:CGRectMake(20, 120, 280, 280)];
       
        //@update: Tune for Orientation
        self.filterListTableView.transform = CGAffineTransformMakeRotation(-M_PI * 2.0);

        [self.filterListTableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, kOverlayTableViewRowHeight)];
    }
    // 設備垂直 180 度擺放
    else if (interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {
        //@update: Tune UI
        [self.filterLensImageView setFrame:CGRectMake(20, 120, 280, 280)];
       
        //@update: Tune for Orientation
        self.filterListTableView.transform = CGAffineTransformMakeRotation(-M_PI * 2.0);

        [self.filterListTableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, kOverlayTableViewRowHeight)];
    }
    // 設備水平向右擺放
    else if (interfaceOrientation == UIDeviceOrientationLandscapeRight) {
        //@update: Tune UI
        [self.filterLensImageView setFrame:CGRectMake(120, 20, 280, 280)];
       
        //@update: Tune for Orientation
        self.filterListTableView.transform = CGAffineTransformMakeRotation(M_PI * 0.5);

        [self.filterListTableView setFrame:CGRectMake(0, 0, kOverlayTableViewRowHeight, self.view.frame.size.width)];
    }
    // 設備水平向左擺放
    else if (interfaceOrientation == UIDeviceOrientationLandscapeLeft) {
        //@update: Tune UI
        [self.filterLensImageView setFrame:CGRectMake(120, 20, 280, 280)];
       
        //@update: Tune for Orientation
        self.filterListTableView.transform = CGAffineTransformMakeRotation(M_PI * 0.5);

        [self.filterListTableView setFrame:CGRectMake(0, 0, kOverlayTableViewRowHeight, self.view.frame.size.width)];
    }
    // 其它 (UIDeviceOrientationFaceUp, UIDeviceOrientationFaceDown, UIDeviceOrientationUnknown)
    else {
        //@update: Tune UI
        [self.filterLensImageView setFrame:CGRectMake(120, 20, 280, 280)];
       
        //@update: Tune for Orientation
        self.filterListTableView.transform = CGAffineTransformMakeRotation(M_PI * 0.5);

        [self.filterListTableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, kOverlayTableViewRowHeight)];
    }
   
    return YES;
}
....

      2. 編譯並執行

--------------------------------------------------------------------------------

B. 調整濾鏡表單水平擺放的 FilterCell

      1. 新增一個類別來放置 global 變數
          a. Xcode > File > New > File...
              > iOS > Cocoa Touch > Objective-C class > Next
            Class: GlobalDataClass
            Subclass of: NSObject
           > Next > Create

           b. 開啓 GlobalDataClass.h 檔案, 修改如下:
#import <Foundation/Foundation.h>

@interface GlobalDataClass : NSObject
{
    //@add: array for storage filterCells
    NSMutableArray *filterCellArray;

}

//@add
@property (nonatomic, strong) NSMutableArray *filterCellArray;

//@add
+ (GlobalDataClass *)getInstance;

@end

          c. 開啓 GlobalDataClass.m 檔案, 修改如下:
#import "GlobalDataClass.h"

@implementation GlobalDataClass

//@add
@synthesize filterCellArray = _filterCellArray;

//@add
+ (GlobalDataClass *)getInstance
{
    static GlobalDataClass *instance = nil;
   
    @synchronized(self)
    {
        if(!instance)
        {
            instance = [[GlobalDataClass alloc] init];
        }
    }  
   
    return instance;
}

@end

---------------------------------------------------------------------------

      2. 開啓 HorizontalTableCell.h 檔案, 修改如下:
#import <UIKit/UIKit.h>
//@add
#import "ConstantDefined.h"
#import "FilterCell.h"

//@add
#import "GlobalDataClass.h"
....

---------------------------------------------------------------------------

      3. 開啓 HorizontalTableCell.m 檔案, 修改如下:
....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
....
    //@title text: we don't need to load that, just read it from our array of filters. 
    cell.titleLabel.text = [currentFilter objectForKey:@"Title"];
   
    //@add for storage filterCells
    //
    // 當沒有找到相同的 titleLabel.text 時, 才將新的 filterCell 存入 Array 裡.

    GlobalDataClass *dataObj = [GlobalDataClass getInstance];

    BOOL saveCell = YES;
    for (FilterCell* filterCell in dataObj.filterCellArray) {
        if ([filterCell.titleLabel.text isEqualToString:cell.titleLabel.text])
        {
            saveCell = NO;
            break;
        }
    }
    if (saveCell) {
        [dataObj.filterCellArray addObject:cell];
    }
    NSLog(@"dataObj.filterCellArray.count = %d", dataObj.filterCellArray.count);
   
    return cell;
}
....
(update: 2012/04/20)

備註:
1. 當找到相同titleLabel.text 時, 如果刪除舊的 filterCell, 程式在執行時,
    於拖拉"濾鏡表單" 時會 crash 掉.

2. 如果一律將 filterCell 儲存起來, 程式在執行時, 於每次拖拉"濾鏡表單" 時,
    都會將新產生的 filterCell (即使 titleLabel.text 名稱相同)存入 Array 裡. 

---------------------------------------------------------------------------

      4. 開啓 ViewController.h 檔案, 修改如下:
....
//@add
#import "Filter4CamHelper.h"
#import "ConstantDefined.h"
#import "HorizontalTableCell.h"
#import "GlobalDataClass.h"
....

---------------------------------------------------------------------------

      5. 開啓 ViewController.m 檔案, 修改如下:
....
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    //@add
    GlobalDataClass *dataObj = [GlobalDataClass getInstance];
    CGFloat filterCellRotationAngle; // filterCell Rotation Angle
   
    //@update
    // 設備垂直擺放
    if (interfaceOrientation == UIDeviceOrientationPortrait)
    {
        //[self.filterLensImageView setFrame:CGRectMake(20, 80, 280, 280)];
        //@update: Tune UI
        [self.filterLensImageView setFrame:CGRectMake(20, 120, 280, 280)];
       
        //@update: Tune for Orientation
        self.filterListTableView.transform = CGAffineTransformMakeRotation(-M_PI * 2.0);
        [self.filterListTableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, kOverlayTableViewRowHeight)];
       
        //@add
        filterCellRotationAngle = M_PI * 0.5;
    }
    // 設備垂直 180 度擺放
    else if (interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {       
        //[self.filterLensImageView setFrame:CGRectMake(20, 80, 280, 280)];
        //@update: Tune UI
        [self.filterLensImageView setFrame:CGRectMake(20, 120, 280, 280)];
       
        //@update: Tune for Orientation
        self.filterListTableView.transform = CGAffineTransformMakeRotation(-M_PI * 2.0);
        [self.filterListTableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, kOverlayTableViewRowHeight)];
       
        //@add
        filterCellRotationAngle = M_PI * 0.5;
    }
    // 設備水平向右擺放
    else if (interfaceOrientation == UIDeviceOrientationLandscapeRight) {       
        //[self.filterLensImageView setFrame:CGRectMake(80, 20, 280, 280)];
        //@update: Tune UI
        [self.filterLensImageView setFrame:CGRectMake(120, 20, 280, 280)];
       
        //@update: Tune for Orientation
        self.filterListTableView.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
        [self.filterListTableView setFrame:CGRectMake(0, 0, kOverlayTableViewRowHeight, self.view.frame.size.width)];
       
        //@add
        filterCellRotationAngle = 0.0;
    }
    // 設備水平向左擺放
    else if (interfaceOrientation == UIDeviceOrientationLandscapeLeft) {       
        //[self.filterLensImageView setFrame:CGRectMake(80, 20, 280, 280)];
        //@update: Tune UI
        [self.filterLensImageView setFrame:CGRectMake(120, 20, 280, 280)];
       
        //@update: Tune for Orientation
        self.filterListTableView.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
        [self.filterListTableView setFrame:CGRectMake(0, 0, kOverlayTableViewRowHeight, self.view.frame.size.width)];
       
        //@add
        filterCellRotationAngle = 0.0;
    }
    // 其它 (UIDeviceOrientationFaceUp, UIDeviceOrientationFaceDown, UIDeviceOrientationUnknown)
    else {
        //[self.filterLensImageView setFrame:CGRectMake(80, 20, 280, 280)];
        //@update: Tune UI
        [self.filterLensImageView setFrame:CGRectMake(120, 20, 280, 280)];
       
        //@update: Tune for Orientation
        self.filterListTableView.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
        [self.filterListTableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, kOverlayTableViewRowHeight)];
       
        //@add
        filterCellRotationAngle = M_PI * 0.5;
    }
   
    //@add
    if (dataObj.filterCellArray.count > 0) {
        for (FilterCell* filterCell in dataObj.filterCellArray) {
            filterCell.transform = CGAffineTransformMakeRotation(filterCellRotationAngle);
        }
    }
   
    return YES;
}
....

---------------------------------------------------------------------------

      6. 編譯並執行:

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。