一个view上的两个按钮有两个tableview的来回切换功能等,用了auto layout

来源:互联网 发布:哈尔滨交友软件 编辑:程序博客网 时间:2024/05/17 05:10

#import "MyPhraseTabViewController.h"


#import "Categories.h"

#import "GlossaryTableViewCell.h"

#import "MBProgressHUD+LJ.h"

#import "PhraseDisplayCell.h"

#import "UIView+Toast.h"

#import "MecTraPreferenceManager.h"


typedef NS_ENUM(NSInteger, TabButtonTag)

{

    TabButtonTagMyPhrase = 1,  //<! マイフレーズ

    TabButtonTagHistory,        //<!日本語履歴

};


@interface MyPhraseTabViewController () <UITableViewDataSource,UITableViewDelegate>

{

    TabButtonTag    selectedTab_;              //<! 選択中のタブ

}

@property (nonatomic,assign)CGSize greetSize;

@property (nonatomic,strong)NSMutableArray *myphrase;

@property (nonatomic,strong)NSMutableArray *history;

@property (nonatomic,strong)NSMutableArray *plusbts;


@property (nonatomic,weak) IBOutletUIButton *buttonMyPhrase;         //<! マイフレーズボタン

@property (nonatomic,weak) IBOutletUIButton *buttonHistory;          //<! 履歴ボタン

@property (nonatomic,weak) IBOutletUIView   *lineView;               //<! 仕切り線

@property (nonatomic,weak) IBOutletUITableView *phraseTableView;     //<! フレーズ表示テーブル

@property (nonatomic,strong) UIImage *plusButtonImageNormal;

@property (nonatomic,strong) UIImage *plusButtonImageHighlighted;


@end


@implementation MyPhraseTabViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    

    self.view.backgroundColor = [UIColorwhiteColor];

    

    // buttons

    self.buttonMyPhrase.tag = TabButtonTagMyPhrase;

    self.buttonHistory.tag = TabButtonTagHistory;

    self.buttonMyPhrase.exclusiveTouch =

    self.buttonHistory.exclusiveTouch = YES;

    // デフォルトはマイフレーズ表示

    [selfchangeButtonWithSelectedButtonTag:TabButtonTagMyPhrase];

    // line

    self.lineView.backgroundColor = [UIColorcustomColorWithRed:105green:204blue:204];

    // table

    self.phraseTableView.dataSource = self;

    self.phraseTableView.delegate = self;

    self.phraseTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    // cell

    [self.phraseTableViewregisterNib:[UINibnibWithNibName:NSStringFromClass([PhraseDisplayCellclass]) bundle:[NSBundlemainBundle]]

               forCellReuseIdentifier:PhraseDisplayCellID];

    self.plusButtonImageNormal = [UIImageimageNamed:@"bt_14_02"];

    self.plusButtonImageHighlighted = [UIImageimageNamed:@"bt_14_02_p"];

    

    //データを取ります

    TranslationDataManager *tdm = [TranslationDataManagersharedInstance];

    

    //日本語履歴

    NSArray *historyList = [tdmgetJaHistoryList];

    for (int i =0;i < historyList.count;i++) {

        [self.historyaddObject:historyList[i]];

    }

    // マイフレーズ

    NSArray *myPhraseList = (NSArray*)[tdmgetMyPhraseList];

    for (int i =0; i < myPhraseList.count;i++){

        [self.myphraseaddObject:myPhraseList[i]];

    }

    // 長押しアクション

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPressGestureRecognized:)];

    [self.phraseTableViewaddGestureRecognizer:longPress];

}


- (void) viewDidAppear:(BOOL)animated

{

    [superviewDidAppear:animated];

//    //先頭表示のためリロード

//    [self.phraseTableView reloadData];

}


- (void)viewWillDisappear:(BOOL)animated

{

    // DBにマイフレーズを保存

    [[TranslationDataManagersharedInstance]setMyPhraseList:(NSArray *)self.myphrase];

    [superviewWillDisappear:animated];

}


# pragma mark - button callbacks

- (void) tabButtonTapped:(id)sender

{

    if(sender ==self.buttonMyPhrase) {

        [selfchangeButtonWithSelectedButtonTag:TabButtonTagMyPhrase];

    }

    else if(sender == self.buttonHistory) {

        [selfchangeButtonWithSelectedButtonTag:TabButtonTagHistory];

    }

    [self.phraseTableViewreloadData];

}


-(void)longPressGestureRecognized:(id)sender

{

    if(selectedTab_ ==TabButtonTagHistory) {

        return;

    }

    UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;

    UIGestureRecognizerState state = longPress.state;

    CGPoint location = [longPresslocationInView:self.phraseTableView];

    NSIndexPath *indexPath = [self.phraseTableViewindexPathForRowAtPoint:location];

    staticUIView   *snapshot = nil;

    staticNSIndexPath *sourceIndexPath =nil;

    switch (state) {

            //移動が始まります

        caseUIGestureRecognizerStateBegan:

            if (indexPath){

                sourceIndexPath = indexPath;

                GlossaryTableViewCell *cell3 = [self.phraseTableViewcellForRowAtIndexPath:indexPath];

                snapshot = [selfcustomSnapshoFromView:cell3];

                __blockCGPoint center = cell3.center;

                snapshot.center = center;

                snapshot.alpha =0.9;

                [self.phraseTableViewaddSubview:snapshot];

                [UIViewanimateWithDuration:0.25animations:^{

                    center.y = location.y;

                    snapshot.center = center;

                    snapshot.transform =CGAffineTransformMakeScale(1.05,1.05);

                    snapshot.alpha =0.9;

                    cell3.hidden =YES;

                    [self.phraseTableViewreloadData];

                }];

            }

            break;

        caseUIGestureRecognizerStateChanged:{

            //移動中

            CGPoint center = snapshot.center;

            center.y = location.y;

            snapshot.center = center;

            if (indexPath && ![indexPathisEqual:sourceIndexPath]){

                [self.myphraseexchangeObjectAtIndex:indexPath.rowwithObjectAtIndex:sourceIndexPath.row];

                

                [self.phraseTableViewmoveRowAtIndexPath:sourceIndexPathtoIndexPath:indexPath];

                  sourceIndexPath = indexPath;

                }

            break;

        }

        default:{

            //移動しました

            GlossaryTableViewCell *cell4 = [self.phraseTableViewcellForRowAtIndexPath:sourceIndexPath];

            cell4.alpha =0.9;

            [UIViewanimateWithDuration:0.25animations:^{

                snapshot.center = cell4.center;

                snapshot.transform =CGAffineTransformIdentity;

                snapshot.alpha =0.9;

                cell4.alpha =1.0;

            }completion:^(BOOL finished) {

                cell4.hidden =NO;

                sourceIndexPath = nil;

                [snapshot removeFromSuperview];

                snapshot = nil;

            }];

            [self.phraseTableViewreloadData];

            break;

        }

    }

}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    LogDebug(@"numberofrow %zd", (selectedTab_ == TabButtonTagMyPhrase) ?_myphrase.count :_history.count);

    return (selectedTab_ ==TabButtonTagMyPhrase) ?_myphrase.count :_history.count;

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    PhraseDisplayCell *cell = [tableViewdequeueReusableCellWithIdentifier:PhraseDisplayCellID];

    cell.accessoryType =UITableViewCellAccessoryNone;

    cell.labelPhrase.textColor = [UIColorcustomColorWithRed:102green:102blue:102];

    cell.labelPhrase.font = [UIFontMecTraFontOfSize:17.0];

    cell.separatorView.backgroundColor = tableView.separatorColor;

    if(selectedTab_ ==TabButtonTagMyPhrase) {

        // マイフレーズ表示

        cell.labelPhrase.text =self.myphrase[indexPath.row];

        [cell setButtonHidden:YES];

    }

    else {

        // 履歴表示

        cell.labelPhrase.text =self.history[indexPath.row];

        [cell.buttonPlussetImage:self.plusButtonImageNormalforState:UIControlStateNormal];

        [cell.buttonPlussetImage:self.plusButtonImageHighlightedforState:UIControlStateHighlighted];

        [cell.buttonPlusaddTarget:selfaction:@selector(addDataToMyPhrase:)forControlEvents:UIControlEventTouchUpInside];

        cell.buttonPlus.tag = indexPath.row;

        [cell setButtonHidden:NO];

    }

    return cell;

}


- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

    LogDebug(@"編集%@",selectedTab_ ==TabButtonTagMyPhrase ? @"可能" :@"不可");

    return (selectedTab_ ==TabButtonTagMyPhrase) ? true : false;

}


# pragma mark - UITableView delegate methods

//tableview1の削除機能を追加する

-(NSArray <UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (selectedTab_ ==TabButtonTagMyPhrase) {

        UITableViewRowAction *deleteRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDestructivetitle:@"削除"handler:^(UITableViewRowAction * action,NSIndexPath *  indexPath) {

            [self.myphraseremoveObjectAtIndex:indexPath.row];

            [self.phraseTableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];

            [self.phraseTableViewreloadData];

        }];

        [[TranslationDataManagersharedInstance]deleteMyPhrase:_myphrase[indexPath.row]];

        return@[deleteRowAction];

    }

    else {        

        returnnil;

    }

}


- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return44;

}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    _dataString = (selectedTab_ ==TabButtonTagMyPhrase) ? _myphrase[indexPath.row] :_history[indexPath.row];

        

    NSNotificationCenter *center = [NSNotificationCenterdefaultCenter];

    [center postNotificationName:@"cesuo"object:_dataString];

    //翻訳中画面を表示する

    MBProgressHUD.langCode =@"ja";

    [MBProgressHUDshowMessage:@""];


    [self.navigationControllerpopToRootViewControllerAnimated:YES];

}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

}


# pragma mark - local methods

- (void) changeButtonWithSelectedButtonTag:(TabButtonTag)tag

{

    if(tag ==TabButtonTagMyPhrase) {

        [self.buttonMyPhrasesetBackgroundImage:[UIImageimageNamed:@"pt_13_01"]forState:UIControlStateNormal];

        [self.buttonMyPhrasesetBackgroundImage:[UIImageimageNamed:@"pt_13_01"]forState:UIControlStateHighlighted];

        [self.buttonHistorysetBackgroundImage:[UIImageimageNamed:@"bt_13_01"]forState:UIControlStateNormal];

        [self.buttonHistorysetBackgroundImage:[UIImageimageNamed:@"bt_13_01_p"]forState:UIControlStateHighlighted];

        // target

        [self.buttonMyPhraseremoveTarget:selfaction:@selector(tabButtonTapped:)forControlEvents:UIControlEventTouchUpInside];

        [self.buttonHistoryaddTarget:selfaction:@selector(tabButtonTapped:)forControlEvents:UIControlEventTouchUpInside];

    }

    else {

        [self.buttonMyPhrasesetBackgroundImage:[UIImageimageNamed:@"bt_14_01"]forState:UIControlStateNormal];

        [self.buttonMyPhrasesetBackgroundImage:[UIImageimageNamed:@"bt_14_01_p"]forState:UIControlStateHighlighted];

        [self.buttonHistorysetBackgroundImage:[UIImageimageNamed:@"pt_14_01"]forState:UIControlStateNormal];

        [self.buttonHistorysetBackgroundImage:[UIImageimageNamed:@"pt_14_01"]forState:UIControlStateHighlighted];

        // target

        [self.buttonMyPhraseaddTarget:selfaction:@selector(tabButtonTapped:)forControlEvents:UIControlEventTouchUpInside];

        [self.buttonHistoryremoveTarget:selfaction:@selector(tabButtonTapped:)forControlEvents:UIControlEventTouchUpInside];

    }

    selectedTab_ = tag;

}


-(UIView *)customSnapshoFromView:(UIView *)inputView

{

    //inputviewimageを作る

    UIGraphicsBeginImageContextWithOptions(inputView.bounds.size,NO, 0);

    [inputView.layerrenderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    //image view

    UIView *snapshot = [[UIImageViewalloc] initWithImage:image];

    snapshot.layer.masksToBounds =NO;

    snapshot.layer.cornerRadius =0.0;

    snapshot.layer.shadowRadius =5.0;

    snapshot.layer.shadowOpacity =1;

    

    return snapshot;

}


- (void) addDataToMyPhrase:(UIButton *)button

{

    //重複はさせない

    for (int i=0;i<_myphrase.count;i++){

        if ([_myphrase[i]compare:_history[button.tag]] ==NSOrderedSame) {

            [self.viewmakeToast:@"マイフレーズに登録済みです。"];

            return;

        }

    }

    

    [self.viewmakeToast:@"マイフレーズに登録しました。"];

    

    //履歴から追加

    [_myphraseaddObject:_history[button.tag]];

    [[TranslationDataManagersharedInstance]addMyPhrase:_history[button.tag]];

    [self.phraseTableViewreloadData];

}


//deleteDataのインスタンスを作る

-(NSMutableArray *)myphrase

{

    if (_myphrase ==nil) {

        _myphrase = [NSMutableArrayarray];

    }

    return_myphrase;

}


//deleteData3のインスタンスを作る

-(NSMutableArray *)history

{

    if (_history ==nil) {

        _history = [NSMutableArrayarray];

    }

    return_history;

}


@end


0 0
原创粉丝点击