質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

0回答

411閲覧

FScalendarでロングタップで日付を取得したい。

koromay

総合スコア0

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2020/06/21 01:24

##FScalendarで日付をロングタップしたら、その日付を取得したいと思っています。
普通のタップで日付やテキストをDBに保存して、ロングタップしたらモーダルウィンドウが立ち上がり日付とテキストの内容を表示するみたいな処理をしたいと思っています。

UILongPressGestureRecognizerで日付をロングタップしてイベントを発生させる事はできたのですが、ロングタップしている日付を取得する方法がわからない状態です。

###試した事
調べたところFScalendarではロングタップ時のメソッドはないようなので、こちらの記事を参考にFScalendarのライブラリを編集しました。

「FSCalendar.h」

swift

1@interface FSCalendar : UIView 2//下記を1行を追加 3@property (readonly, nonatomic) BOOL isLongPressGesture; 4

「FSCalendar.m」プロパティの設定値の追加

swift

1- (void)handleSwipeToChoose:(UILongPressGestureRecognizer *)pressGesture 2{ 3 switch (pressGesture.state) { 4 case UIGestureRecognizerStateBegan: 5 case UIGestureRecognizerStateChanged: { 6       //下記を1行を追加 7 _isLongPressGesture = YES; 8 9 NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[pressGesture locationInView:self.collectionView]]; 10 if (indexPath && ![indexPath isEqual:self.lastPressedIndexPath]) { 11 NSDate *date = [self.calculator dateForIndexPath:indexPath]; 12 FSCalendarMonthPosition monthPosition = [self.calculator monthPositionForIndexPath:indexPath]; 13 if (![self.selectedDates containsObject:date] && [self collectionView:self.collectionView shouldSelectItemAtIndexPath:indexPath]) { 14 [self selectDate:date scrollToDate:NO atMonthPosition:monthPosition]; 15 [self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath]; 16 } else if (self.collectionView.allowsMultipleSelection && [self collectionView:self.collectionView shouldDeselectItemAtIndexPath:indexPath]) { 17 [self deselectDate:date]; 18 [self collectionView:self.collectionView didDeselectItemAtIndexPath:indexPath]; 19 } 20 } 21 self.lastPressedIndexPath = indexPath; 22 break; 23 } 24 case UIGestureRecognizerStateEnded: 25 case UIGestureRecognizerStateCancelled: { 26 self.lastPressedIndexPath = nil; 27 break; 28 } 29 default: 30 break; 31 } 32 33} 34

「FSCalendar.m」メソッドの変更

swift

1- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 2{ 3 NSDate *selectedDate = [self.calculator dateForIndexPath:indexPath]; 4 FSCalendarMonthPosition monthPosition = [self.calculator monthPositionForIndexPath:indexPath]; 5 FSCalendarCell *cell; 6 if (monthPosition == FSCalendarMonthPositionCurrent) { 7 cell = (FSCalendarCell *)[collectionView cellForItemAtIndexPath:indexPath]; 8 } else { 9 cell = [self cellForDate:selectedDate atMonthPosition:FSCalendarMonthPositionCurrent]; 10 NSIndexPath *indexPath = [collectionView indexPathForCell:cell]; 11 if (indexPath) { 12 [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone]; 13 } 14 } 15 if (![_selectedDates containsObject:selectedDate]) { 16 cell.selected = YES; 17 [cell performSelecting]; 18 } 19 [self enqueueSelectedDate:selectedDate]; 20 [self.delegateProxy calendar:self didSelectDate:selectedDate atMonthPosition:monthPosition]; 21 [self selectCounterpartDate:selectedDate]; 22 //下記1行を追加 23 _isLongPressGesture = NO; 24}

使い方がわからず困っています

参考記事には下記コードで使えるような感じで書いていて、初心者なりに「if文でロングタップの時と、普通のタップの時と処理を分けて使うのかな」って思っているのですが、具体的にどのように使えばいいかご教授お願いします。

swift

1- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition 2{ 3 if (calendar.isLongPressGesture) 4 NSLog(@"Long Press"); 5 else 6 NSLog(@"Tap Gesture"); 7}

的外れかもしれませんが、自分なりにこんな感じで使うのかなって試してみました。
シュミレーターで立ち上げて日付をロングタップするとエラーになります。(Thread 1: EXC_BAD_ACCESS (code=1, address=0x1))

swift

1import UIKit 2import FSCalendar 3import RealmSwift 4 5 6class ViewController: UIViewController,FSCalendarDataSource,FSCalendarDelegate { 7 8 9 @IBOutlet weak var myCalendar: FSCalendar! 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 let recognizer = UILongPressGestureRecognizer(target: self, action: #selector(calendar2)) 15 self.myCalendar.addGestureRecognizer(recognizer) 16 } 17 18 19 @objc func calendar2(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) { 20 21 if myCalendar.isLongPressGesture { 22 //ここにロングタップの時の処理 23 } else { 24 //ここに普通のタップの時の処理 25 } 26 }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問