• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:Objective-Cの繰返しアニメーション)

Objective-Cの繰り返しアニメーションで画像を点滅させる方法

このQ&Aのポイント
  • [UIView animateWithDuration: 1 delay: 0.0 options: UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations: ^{image.alpha = 0;} completion: ^(BOOL finished){image.alpha = 0;}]; 上のコードは、Objective-Cで画像を永遠に点滅させる繰り返しアニメーションのコードです。
  • しかし、このコードでは画像を3回のみ点滅させるようにすることができません。
  • 入れ子構造を使えば複雑な方法で実装できますが、よりスッキリしたコードを書く方法があるので教えてほしいとのことです。

質問者が選んだベストアンサー

  • ベストアンサー
  • harawo
  • ベストアンサー率58% (3742/6450)
回答No.1

[UIView animateWithDuration: 1.0 // この引数の型は、実数型(NSTimeInterval)なので delay: 0.0 options: UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations: ^{[UIView setAnimationRepeatCount: 3.0]; // 追加。 image.alpha = 0.0;} // プロパティalphaの型も実数型(CGFloat)なので completion: ^(BOOL finished){image.alpha = 0.0;} // おなじく ]; このように変更してください。つまり、UIViewのクラスメソッド「setAnimationRepeatCount:」で、繰り返し回数を指定します。

narative_h
質問者

お礼

毎度ありがとうございます。 解決いたしました。 setAnimationRepeatCount  であるだろうということは 意識していたのですが、 自分の記述する場所が違っていました。 ふぅ…。

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • [Objective-C]アニメーション

    Xcode 4.5.2を使用して、Objective-CでiPhoneアプリの開発を行っています。 UIView(viewAとします)のsubViewとして 3つのUIButton(btn1, btn2, btn3とします)を配置し、 「animateWithDuration:delay:options:animations:completion:」メソッドを使用して viewAごと移動させるアニメーションを実行しようとしています。 btn1~btn3のタップイベントを実装したいのですが アニメーション実行中はbtn1~btn3のタップイベントが受け付けられません。 「animateWithDuration:delay:options:animations:completion:」メソッドの「option:」に 「UIViewAnimationOptionAllowUserInteraction」を指定してみたのですが それでもだめです。 原因が分かる方、いらっしゃいましたら教えていただけないでしょうか? 初歩的な質問で申し訳ありません。。 (ソース) ※画面はstoryboardで作成しています。 //アニメーション設定 [UIView animateWithDuration:5.0f delay:0.5 options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionAllowUserInteraction animations:^{ //移動 [self.viewA setFrame:CGRectMake(0, 330, 320, 60)]; } completion:^(BOOL finished) { //処理 } ];

  • Xcode - 連続したアニメーションの繰り返し

    現在、Objective-Cにて、2つの連続した動作をするアニメーションを作っていますが、 この2つのアニメーションを永続的に繰り返すことができず困っています。 下記のようにanimateWithDurationで1つ目のアニメーションを行い、complation内で2つ目のアニメーションを入れ子にして実行しています。 [UIView animateWithDuration:0.5f delay:0.5f options:UIViewAnimationCurveLinear animations:^{ // 1つ目のアニメーション : : } completion:^(BOOL finished) { [UIView animateWithDuration:0.5f delay:0.5f options:UIViewAnimationCurveLinear animations:^{ // 2つ目のアニメーション : : } completion:^(BOOL finished) { // 終了 }]; }]; このままですと、1つ目と2つ目のアニメーションを実行し終えた後そのまま終了してしまうので、optionsにUIViewAnimationOptionRepeatを入れて見ましたが、1つのアニメーションしか繰り返してくれません。 1つ目と2つ目を連続で繰り返す方法を教えていただければと存じます。 どうぞよろしくお願いいたします。

  • Objective-Cのメソッド効率化

    下のコードは UIButton のbutton1を押すと そのボタン自身が一回だけ10px上に移動し、 戻るコードです。 例えばこのボタンがひとつだけでなく 10個ある場合を考えて - (IBAction)move2 { int x = button2.center.x; int y = button2.center.y; ~~~ } と10個記述していくのではなく、 うまく関数化したいのですが どのような式を考えたら良いのでしょうか? よろしくお願い致します。 - (IBAction)move1 { int x = button1.center.x; int y = button1.center.y; [UIView animateWithDuration:0.2f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ button1.center = CGPointMake(x, y-10); } completion:^(BOOL finished){ [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ button1.center = CGPointMake(x, y); } completion:^(BOOL finished){ }]; }]; }

  • スクロールの上でラベルをドラッグする方法について

    現在、左右スクロールと上下フリックができるViewをタップすることでラベルがでてきて、そのラベルをドラッグできるようなコードを作っています。 具体的には、ViewController のViewの上に、画像を左右にScrollできるView(ScrollView)をのせ、その上に、それらの画像を上下にフリックすると別の画像がでてくるようなView(PageView、親クラスがUISrollView)を作成しています。 そこで、上下のフリック、スクロール、タップでラベルがでてくるのはできるようになったのですが、ラベルを認識してドラッグができる(UIPanGestureRecognizer)というのができません。 もし、おわかりになられる方がいらっしゃいましたら、お手数をおかけしますがご教授頂けたら幸いです。 以下、コードです。 xcodeは5.0です。 これらはViewContorllerの中に書いてあります。 //ScrollViewの上にPageViewクラスで作ったpageを乗せてスクロールしています。pageには画像をいれています。 for (int i=1;i<4;i++) { PageView *page = [[PageView alloc] initWithFrame:self.view.bounds]; page.delegate=self; [page setImage:[NSString stringWithFormat:@"%d",i]]; page.frame = CGRectMake( self.view.frame.size.width * (i-1), 0, self.view.frame.size.width, self.view.frame.size.height ); [scrollView addSubview:page]; //pageにタップしてproductLabelがでてくるようにしています。 [page addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrlTapREcotTap:)]]; NSLog(@"tap"); //ここでラベルのproductLabelだけに触れてドラッグできるようにしたいのですが、ドラッグができません。 UIPanGestureRecognizer *pan =[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)]; [productLabel addGestureRecognizer:pan]; NSLog(@"drag"); }} //ラベルをタップにより表示したり、非表示にしたりしています。 - (void)scrlTapREcotTap:(UITapGestureRecognizer *)gestureRecognizer { if(isTappFirstTime) { //put code of hide [UIView animateWithDuration:1 animations:^{ productLabel.alpha = 0; } completion: ^(BOOL finished) { productLabel.hidden = YES; }]; isTappFirstTime = NO; NSLog(@"非表示"); } else { // put code of show [UIView animateWithDuration:1 animations:^{ productLabel = [[UILabel alloc] init]; //UILabel *label = [[UILabel alloc] init]; productLabel.frame = CGRectMake(10, 10, 100, 50); productLabel.backgroundColor = [UIColor yellowColor]; productLabel.textColor = [UIColor blueColor]; productLabel.font = [UIFont fontWithName:@"AppleGothic" size:12]; productLabel.textAlignment = UITextAlignmentCenter; productLabel.text = @"hoge"; [scrollView addSubview:productLabel]; (NSLog(@"表示")); productLabel.alpha = 1; } completion: ^(BOOL finished) { productLabel.hidden = NO; }]; isTappFirstTime = YES; } } //productLabelだけがドラッグできるようにしています。 - (void) handlePanGesture:(UIPanGestureRecognizer*) sender { CGPoint location = [sender translationInView:productLabel]; CGPoint movedPoint = CGPointMake(productLabel.center.x + location.x, productLabel.center.y + location.y); productLabel.center = movedPoint; NSLog(@"dragmethod"); [sender setTranslation:CGPointZero inView:productLabel]; } お手数おかけしますが、宜しくお願いします。

  • Xcodeで開発してますが。。

    CGAffineTransformMakeScalの動きがおかしいです。 下記コード一部抜粋しています。5行目です。 Xcodeは Version 5.1.1 です ----- ボタンが押されると、numericButtonPressedを実行し、途中で [self animateButton:button];により拡大/縮小のアニメーションをつけるプログラムです。 ----- アニメーションでX方向/Y方向に2倍拡大はできるのですが、左上からの座標も指定した分だけ ずれてしまいます。 button.center = CGPointMake(0, 0);を挿入したりしてもダメです。 CGAffineTransformMakeScaleの仕様なのか、プログラムがおかしいのかわかりません どう対処すればよいか、教えてください。 ◆◆◆◆コード一部抜粋 - (void)animateButton:(UIButton*)button { [UIView animateWithDuration:1 animations:^{ // 位置までずれてしまう。。。 button.transform = CGAffineTransformMakeScale(2, 2); } completion:^(BOOL complete){ [UIView animateWithDuration:1 animations:^{ button.transform = CGAffineTransformIdentity; } ]; } ]; } - (IBAction)numericButtonPressed:(id)sender { UIButton *button = (UIButton*)sender; [self animateButton:button]; // 拡大/縮小のアニメーション NSString *numericChar = button.titleLabel.text; int number = [numericChar intValue]; if ([opString isEqualToString:@"="]) { calcValue = 0; opString = nil; } calcValue = calcValue * 10 + number; opApplied = NO; [self updateResultLabel]; } ◆◆◆◆

  • c言語でpicにDelay関数を使いたい

    pic16f84a用にLED点滅のソースコードを書きましたがエラー出て前に進めません。 コードは次の様になっています。 (前段略) #use delay(clock=20000000) void main(void) { TRISA = 0x00; PORTA = 0x03; TRISB = 0x00; PORTB = 0x00; while (1) { PORTA = 0x02; Delay_ms(1000); PORTA = 0x01; Delay_(1000); } } コード書き込み中にすでにDelayの箇所が2か所ともエラーの表示になってしまいます。 一応下記の様にdelya箇所をコメント化してビルドと,picに書き込みを行えます、そしてLEDも点滅できます。 while (1) { PORTA = 0x02; //Delay_ms(1000); PORTA = 0x01; //Delay_(1000); } } 参考のコードを正確に書き込みましたがこのような状況でしたので、   #use delay(clock=20000000) は自分が勝手に追加してみましたが結果はやはり同じでした。 何が原因なのでしょうか。 何方か教えて頂けませんか。お願いいたします。 自分の環境は、Windows8 MPLABX IDE2.10  XC8  PIC16F84A pickit3 です。

  • objective-C のOCR認識のサンプル

    http://blog.isana.net/2014/08/ios.html 上記サイトのサンプルコードをそのままコピーしたのですが、以下のエラーが出てしまうのですがなぜでしょうか。 No visible @interface for "UIView" declares the selector "setImage" 下記コードの、// イメージビューに画像をセットとコメントしてある部分です。回答をお願いします // 中略 // カメラボタン押下 - (IBAction)tappedCamera:(id)sender { UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; // デリゲート指定 [imagePickerController setDelegate:self]; // トリミング指定 [imagePickerController setAllowsEditing:YES]; // カメラの使用有無を確認 if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) { // カメラを指定 [imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera]; } else { // アルバムを指定 [imagePickerController setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum]; } // コントローラ起動 [self presentViewController:imagePickerController animated:YES completion:nil]; } // コントローラ終了 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // イメージをメモリに保存 self.selectedImage = [info objectForKey:UIImagePickerControllerEditedImage]; // イメージビューに画像をセット [self.imageView setImage:self.selectedImage]; // 親ビューへ戻る [self dismissViewControllerAnimated:YES completion:nil]; // テキストを空に [self.textView setText:nil]; // インジケータ開始 UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; spinner.center = CGPointMake(160, 240); spinner.hidesWhenStopped = YES; [self.view addSubview:spinner]; [spinner startAnimating]; // OCR実行 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // 英語 : eng を設定 (日本語の場合は jpn を指定) Tesseract* tesseract = [[Tesseract alloc] initWithLanguage:@"eng"]; // OCRを実行する画像を設定 [tesseract setImage:self.selectedImage]; // OCR実行 [tesseract recognize]; // 実行結果をアラートビューで表示 dispatch_async(dispatch_get_main_queue(), ^{ // 結果をテキストビューに指定 [self.textView setText:[tesseract recognizedText]]; // インジケータ停止 [spinner stopAnimating]; }); }); }

  • [再]objective-c 描画処理について

    前回質問させて頂きましたが、まだ抜け出せていないので教えて下さい。 ↓↓↓ -------------------------------------------------------------------------------------------------------------- 初心者ですが、iPhoneアプリを作成しています。 画像を並べてスクロールできるように配列で描画処理を行ないたいのですが、うまくいきません。 今のところのコードはこんな感じです↓ - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray* imageList = [NSMutableArray array]; for (int i=1; i < 20; i++) { UIImage* image = [UIImage imageNamed: [NSString stringWithFormat:@"%d.jpg", i+1]]; [imageList addObject:image]; } MyImageView* imageView = [[MyImageView alloc] initWithImage:imageList]; scrollView.contentSize = imageView.bounds.size; [scrollView addSubview:imageView]; [imageView release]; } どなたか分かる方、教えて頂けますよう宜しくお願いします。 -------------------------------------------------------------------------------------------------------------- ↓ NSArray *initWithImage;と引数で渡しましたが、 「MyImageView* imageView = [[MyImageView alloc] initWithImage:imageList];」 の部分で、「Incompatible pointer sending 'NSMutableArray *' to parameter of type 'UIImage *'」とエラー表示され、実行すると、 「Tread1 : Program received signal: "SIGABRT"」で落ちてしまいます。

  • [objective-c]アニメーション表示

    UIViewのアニメーションで、 「12時の地点から、時計回りに徐々に表示する」 という物を実装したいのですが、どのように実装すれば良いでしょうか? 色々調べて見たのですが、フェードインやスライドインの アニメーションしか見つからず、上記のようなアニメーションを 行う方法が見つかりませんでした。 ご教授よろしくお願いいたします。

  • ios/初回起動時に表示させる画面

    初回起動時にだけ、WelcomeViewControllerというViewControllerを表示させたいのですが、うまくいきません Is Initial View Controllerに設定してあるFirstViewControllerの.mファイルに以下のコードを書きました -(void)viewWillAppear:(BOOL)animated{ BOOL HasLaunchedOnce = [[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]; if (HasLaunchedOnce == NO) { // 初回起動時 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"]; [[NSUserDefaults standardUserDefaults] synchronize]; WelcomeViewController *welcomeViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"welcome"]; [self presentViewController:welcomeViewController animated:YES completion:nil]; } }