一个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
{
//inputviewにimageを作る
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
- 一个view上的两个按钮有两个tableview的来回切换功能等,用了auto layout
- 一个tableView的Cell中有两个点击按钮及tableView的重用
- tableView上的按钮功能
- 两个图片来回切换
- android 两个放置VideoView的Actitity来回切换BUG
- 如何在一个view上 显示两个tableview
- 如何在一个view上 显示两个tableview
- iOS如何在一个view上 显示两个tableview
- 关于一个页面里面有两个按钮的解决。
- 弹出框,有两个,自定义一个view和系统的
- JavaScript 两个按钮,实现图片的切换
- 按钮的两个图片切换效果
- 两个按钮之间的切换(单选)
- 终于搞定UIScrollView上的几个tableview和collection view之间切换的问题了
- 怎么用java代码写一个线性布局;布局里面有两个按钮是水平的
- js点击按钮更改html页面的图片(两个来回换)
- 有两个按钮的wxWidgets小程序
- 多表-连表筛选代码(其中一个view有两个表格tableView)
- 欢迎使用CSDN-markdown编辑器
- Kali Linux渗透测试实战 2.1 DNS信息收集
- Android之Fragment初窥门径(接口的简单流程)
- UltraEdit23.20.0.34安装注册以及添加Verilog和VHDL的高亮显示
- Kali Linux渗透测试实战 2.2 操作系统指纹识别
- 一个view上的两个按钮有两个tableview的来回切换功能等,用了auto layout
- markdownpad2注册码
- .NET基础加强笔记
- hdu5761-Rower Bo(大学物理第一章对速度积分)
- 【iOS】The run destination iPhone is not valid for Running the scheme .
- 回到顶部,当页面下拉到一定高度时,就会出现回到顶部的按钮,点击回到顶部之后,会有一个速度的变化回滚到顶部,如果正在回到顶部时,鼠标的滚轮转动了就会停止回到顶部
- Java反射
- Sightseeing_poj3463_dijkstra
- 浅析Java集合类源码(二)--- HashSet, HashMap, Hashtable