您的位置:首页 > 其它

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

2016-09-16 12:29 465 查看
#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) IBOutlet
UIButton *buttonMyPhrase;         
//<! マイフレーズボタン

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

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

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

@property (nonatomic,
strong) UIImage *plusButtonImageNormal;

@property (nonatomic,
strong) UIImage *plusButtonImageHighlighted;

@end

@implementation MyPhraseTabViewController

- (void)viewDidLoad

{

    [super
viewDidLoad];

    

    self.view.backgroundColor = [UIColor
whiteColor];

    

    // buttons

    self.buttonMyPhrase.tag
= TabButtonTagMyPhrase;

    self.buttonHistory.tag
= TabButtonTagHistory;

    self.buttonMyPhrase.exclusiveTouch
=

    self.buttonHistory.exclusiveTouch
= YES;

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

    [self
changeButtonWithSelectedButtonTag:TabButtonTagMyPhrase];

    // line

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

    // table

    self.phraseTableView.dataSource
= self;

    self.phraseTableView.delegate
= self;

    self.phraseTableView.separatorStyle
= UITableViewCellSeparatorStyleNone;

    // cell

    [self.phraseTableView
registerNib:[UINib
nibWithNibName:NSStringFromClass([PhraseDisplayCell
class]) bundle:[NSBundle
mainBundle]]

               forCellReuseIdentifier:PhraseDisplayCellID];

    self.plusButtonImageNormal = [UIImage
imageNamed:@"bt_14_02"];

    self.plusButtonImageHighlighted = [UIImage
imageNamed:@"bt_14_02_p"];

    

   
//データを取ります

    TranslationDataManager *tdm = [TranslationDataManager
sharedInstance];

    

    //日本語履歴

    NSArray *historyList = [tdm
getJaHistoryList];

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

        [self.history
addObject:historyList[i]];

    }

    // マイフレーズ

    NSArray *myPhraseList = (NSArray*)[tdm
getMyPhraseList];

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

        [self.myphrase
addObject:myPhraseList[i]];

    }

   
// 長押しアクション

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer
alloc] initWithTarget:self
action:@selector(longPressGestureRecognized:)];

    [self.phraseTableView
addGestureRecognizer:longPress];

}

- (void) viewDidAppear:(BOOL)animated

{

    [super
viewDidAppear:animated];

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

//    [self.phraseTableView reloadData];

}

- (void)viewWillDisappear:(BOOL)animated

{

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

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

    [super
viewWillDisappear:animated];

}

# pragma mark - button callbacks

- (void) tabButtonTapped:(id)sender

{

    if(sender ==
self.buttonMyPhrase) {

        [self
changeButtonWithSelectedButtonTag:TabButtonTagMyPhrase];

    }

    else
if(sender == self.buttonHistory) {

        [self
changeButtonWithSelectedButtonTag:TabButtonTagHistory];

    }

    [self.phraseTableView
reloadData];

}

-(void)longPressGestureRecognized:(id)sender

{

    if(selectedTab_ ==
TabButtonTagHistory) {

        return;

    }

    UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;

    UIGestureRecognizerState state = longPress.state;

    CGPoint location = [longPress
locationInView:self.phraseTableView];

    NSIndexPath *indexPath = [self.phraseTableView
indexPathForRowAtPoint:location];

    static
UIView   *snapshot =
nil;

    static
NSIndexPath *sourceIndexPath =
nil;

    switch (state) {

            //移動が始まります

        case
UIGestureRecognizerStateBegan:

            if (indexPath){

                sourceIndexPath = indexPath;

                GlossaryTableViewCell *cell3 = [self.phraseTableView
cellForRowAtIndexPath:indexPath];

                snapshot = [self
customSnapshoFromView:cell3];

                __block
CGPoint center = cell3.center;

                snapshot.center = center;

                snapshot.alpha =
0.9;

                [self.phraseTableView
addSubview:snapshot];

                [UIView
animateWithDuration:0.25
animations:^{

                    center.y = location.y;

                    snapshot.center = center;

                    snapshot.transform =
CGAffineTransformMakeScale(1.05,
1.05);

                    snapshot.alpha =
0.9;

                    cell3.hidden =
YES;

                    [self.phraseTableView
reloadData];

                }];

            }

            break;

        case
UIGestureRecognizerStateChanged:{

            //移動中

            CGPoint center = snapshot.center;

            center.y = location.y;

            snapshot.center = center;

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

                [self.myphrase
exchangeObjectAtIndex:indexPath.row
withObjectAtIndex:sourceIndexPath.row];

                

                [self.phraseTableView
moveRowAtIndexPath:sourceIndexPath
toIndexPath:indexPath];

                  sourceIndexPath = indexPath;

                }

            break;

        }

        default:{

            //移動しました

            GlossaryTableViewCell *cell4 = [self.phraseTableView
cellForRowAtIndexPath:sourceIndexPath];

            cell4.alpha =
0.9;

            [UIView
animateWithDuration:0.25
animations:^{

                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.phraseTableView
reloadData];

            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 = [tableView
dequeueReusableCellWithIdentifier:PhraseDisplayCellID];

    cell.accessoryType =
UITableViewCellAccessoryNone;

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

    cell.labelPhrase.font = [UIFont
MecTraFontOfSize: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.buttonPlus
setImage:self.plusButtonImageNormal
forState:UIControlStateNormal];

        [cell.buttonPlus
setImage:self.plusButtonImageHighlighted
forState:UIControlStateHighlighted];

        [cell.buttonPlus
addTarget:self
action:@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 = [UITableViewRowAction
rowActionWithStyle:UITableViewRowActionStyleDestructive
title:@"削除"
handler:^(UITableViewRowAction * action,
NSIndexPath *  indexPath) {

            [self.myphrase
removeObjectAtIndex:indexPath.row];

            [self.phraseTableView
deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];

            [self.phraseTableView
reloadData];

        }];

        [[TranslationDataManager
sharedInstance]
deleteMyPhrase:_myphrase[indexPath.row]];

        return
@[deleteRowAction];

    }

    else {        

        return
nil;

    }

}

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

{

    return
44;

}

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

{

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

        

    NSNotificationCenter *center = [NSNotificationCenter
defaultCenter];

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

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

    MBProgressHUD.langCode =
@"ja";

    [MBProgressHUD
showMessage:@""];

    [self.navigationController
popToRootViewControllerAnimated:YES];

}

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

{

}

# pragma mark - local methods

- (void) changeButtonWithSelectedButtonTag:(TabButtonTag)tag

{

    if(tag ==
TabButtonTagMyPhrase) {

        [self.buttonMyPhrase
setBackgroundImage:[UIImage
imageNamed:@"pt_13_01"]
forState:UIControlStateNormal];

        [self.buttonMyPhrase
setBackgroundImage:[UIImage
imageNamed:@"pt_13_01"]
forState:UIControlStateHighlighted];

        [self.buttonHistory
setBackgroundImage:[UIImage
imageNamed:@"bt_13_01"]
forState:UIControlStateNormal];

        [self.buttonHistory
setBackgroundImage:[UIImage
imageNamed:@"bt_13_01_p"]
forState:UIControlStateHighlighted];

        // target

        [self.buttonMyPhrase
removeTarget:self
action:@selector(tabButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];

        [self.buttonHistory
addTarget:self
action:@selector(tabButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];

    }

    else {

        [self.buttonMyPhrase
setBackgroundImage:[UIImage
imageNamed:@"bt_14_01"]
forState:UIControlStateNormal];

        [self.buttonMyPhrase
setBackgroundImage:[UIImage
imageNamed:@"bt_14_01_p"]
forState:UIControlStateHighlighted];

        [self.buttonHistory
setBackgroundImage:[UIImage
imageNamed:@"pt_14_01"]
forState:UIControlStateNormal];

        [self.buttonHistory
setBackgroundImage:[UIImage
imageNamed:@"pt_14_01"]
forState:UIControlStateHighlighted];

        // target

        [self.buttonMyPhrase
addTarget:self
action:@selector(tabButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];

        [self.buttonHistory
removeTarget:self
action:@selector(tabButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];

    }

    selectedTab_ = tag;

}

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

{

    //inputviewにimageを作る

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

    [inputView.layer
renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image =
UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    //image view

    UIView *snapshot = [[UIImageView
alloc] 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.view
makeToast:@"マイフレーズに登録済みです。"];

            return;

        }

    }

    

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

    

    //履歴から追加

    [_myphrase
addObject:_history[button.tag]];

    [[TranslationDataManager
sharedInstance]
addMyPhrase:_history[button.tag]];

    [self.phraseTableView
reloadData];

}

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

-(NSMutableArray *)myphrase

{

    if (_myphrase ==
nil) {

        _myphrase = [NSMutableArray
array];

    }

    return
_myphrase;

}

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

-(NSMutableArray *)history

{

    if (_history ==
nil) {

        _history = [NSMutableArray
array];

    }

    return
_history;

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐