TwitterアプリはiPhoneのタブバーアプリです。どのタブも回転しませんが、ツイートからリンクをクリックすると、その上にプッシュされたViewControllerが回転できます。私がこれまでに行った唯一の回転は、デバイス、横向き、または縦向きを傾けることですが、2D変換とアニメーションを使用してビューを回転させる方法がわかりません。
UIViewのサブクラスであるビューをどのように回転させますか?
TwitterアプリはiPhoneのタブバーアプリです。どのタブも回転しませんが、ツイートからリンクをクリックすると、その上にプッシュされたViewControllerが回転できます。私がこれまでに行った唯一の回転は、デバイス、横向き、または縦向きを傾けることですが、2D変換とアニメーションを使用してビューを回転させる方法がわかりません。
UIViewのサブクラスであるビューをどのように回転させますか?
UIViewアニメーションとTransformメソッドを使用できます。90度回転の場合:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
[UIView commitAnimations];
ねえ、私は回転に関して同様の問題を抱えていました。縦向きのままにしておきたいタブバーアプリがありましたが、すべての回転を許可するビデオプレーヤーがありました。
タブバーをサブクラス化し、すべての回転を許可するルートビューコントローラーにビデオプレーヤーを接続している間は縦向きの回転のみを許可することで、これをかなり簡単に行うことができました。ここで私の元の投稿をチェックしてください。助けてくれることを願っています!
このチュートリアルを確認してください。これがあなたがやりたかったことだと思い
ますhttp://www.raywenderlich.com/9864/how-to-create-a-rotating-wheel-control-with-uikithttp://www.raywenderlich
。 com / 5478 /uiview-animation-tutorial-practical-recipes
これは私にとって最高で完璧な仕事であり、これは正しい答えだと思います。
toValueの50の代わりに回転させたい回数を数に指定するだけです
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
NSNumber *currentAngle = [CircleView.layer.presentationLayer valueForKeyPath:@"transform.rotation"];
rotationAnimation.fromValue = currentAngle;
rotationAnimation.toValue = @(50*M_PI);
rotationAnimation.duration = 50.0f; // this might be too fast
rotationAnimation.repeatCount = HUGE_VALF; // HUGE_VALF is defined in math.h so import it
[CircleView.layer addAnimation:rotationAnimation forKey:@"rotationAnimationleft"];
Ref From 向きの変更に応じてUIViewControllerの回転を実装する方法は?
これを行うには、ルートビューコントローラー(これはUITabBarControllerの場合があります)を使用し、そのviewDidLoadメソッドで回転イベントをサブスクライブします。
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification"
object:nil];
次に、didRotate:メソッドで、回転が発生したときに表示されるView Controllerと、電話の向きを確認します。
- (void) didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
/*
DEVICE JUST ROTATED TO PORTRAIT MODE
orientation == UIDeviceOrientationFaceUp ||
orientation == UIDeviceOrientationFaceDown
*/
if(orientation == UIDeviceOrientationPortrait) {
/*
DEVICE JUST ROTATED TO LANDSCAPE MODE
*/
}else if(orientation == UIInterfaceOrientationLandscapeLeft ||
orientation == UIInterfaceOrientationLandscapeRight) {
}
}
そのdidRotate内で:表示されているviewControllerを確認し、そこから必要な処理を実行できます。
特定のビューコントローラが表示され、電話が横向きに回転しているときに、モーダルビューコントローラを横向きモードで表示します。他のViewControllerが表示されている場合は、イベントを無視します。
モーダルビューコントローラーをそのviewWillAppearメソッドで横向きモードで表示するように強制します-必要に応じて、このコードを誰にでも与えることができます。
お役に立てれば。
shouldAutorotateToInterfaceOrientationメソッドを実装できます。Twitterアプリでは、tabBarControllerの縦向きのみが許可されていると思いますが、このメソッドを使用すると、modalViewControllerで横向きが許可されます。ドキュメントで調べてみてください。とても簡単だと思います。これがあなたの質問に答えることを願っています!
@ Mangesh-Vyasにはいくつかの良い情報がありますが、(私が読んだように)質問に完全には答えていません。Twitter(他のアプリの中でも)が表示するビューが回転できるのに、タブバーやナビゲーションバーが回転できないのは、モーダルで表示されるためです。モーダルで表示されるビューは、それらを表示したコントローラーの許容可能な方向に制限されません。
編集:UIViewController
おそらくあなたは株が回転する
ことを期待しています。もしそうなら、(私の知る限りでは)カスタムサブクラスのみを任意のinterfaceOrientationsに回転させることができるので、あなたはがっかりするでしょう。次のコードでうまくいきます。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
編集2:これがすべてを動かす
コードです:
AppDelegate.m
#import "RotateViewController.h"
@interface AppDelegate () {
UINavigationController* nc;
}
- (void)pushVC;
- (void)openVC;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController* vc = [[UIViewController alloc] init];
vc.view.backgroundColor = [UIColor blueColor];
vc.title = @"Root";
UIButton* b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(50, 150, 220, 40);
[b.titleLabel setText:@"Push VC"];
[b addTarget:self action:@selector(pushVC) forControlEvents:UIControlEventTouchUpInside];
[vc.view addSubview:b];
b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
b.frame = CGRectMake(50, 200, 220, 40);
[b.titleLabel setText:@"Modal VC"];
[b addTarget:self action:@selector(openVC) forControlEvents:UIControlEventTouchUpInside];
[vc.view addSubview:b];
nc = [[UINavigationController alloc] initWithRootViewController:vc];
UITabBarController* tc = [[UITabBarController alloc] init];
[tc addChildViewController:nc];
// uncomment this line to see this work in a nav controller
// [self.window setRootViewController:nc];
[self.window setRootViewController:tc];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void)openVC {
RotateViewController* rc = [[RotateViewController alloc] init];
[nc presentModalViewController:rc animated:YES];
}
- (void)pushVC {
RotateViewController* rc = [[RotateViewController alloc] init];
[nc pushViewController:rc animated:YES];
}
RotateViewControllerは、上記の関数 をUIViewController
使用したのストックサブクラスです。Nota beneは、子ビューコントローラのすべてがその方向に戻る場合にのみ、指定されたインターフェイスの方向に回転できます。 トップビューコントローラがそのインターフェイスの向きに戻った場合、特定のインターフェイスの向きに回転できます。shouldAutorotate
UITabBarControllers
YES
UINavigationViewControllers
YES