【问题标题】:My subview doesn't autoSized after a rotation我的子视图在旋转后没有自动调整大小
【发布时间】:2012-07-15 17:47:15
【问题描述】:

我正在尝试在 iPad 上制作一个简单的应用程序。现在我只在自定义视图中显示全屏图片。我的问题是当 iPad 旋转时我的视图没有自动调整大小......旋转效果很好,但我的图像大小仍然相同。

我的应用是基于this,因为我在切换图片的时候想要这个效果。基本上,产生效果的视图是FlipView,它继承了GenericAnimationView 的形式,它继承了UIView 的形式。还有 AnimationFrame 继承自 NSObject ,这是显示 1 个动画周期所必需的。最后,AnimationDelegate 继承自 NSObject,它处理来自转换操作的回调。

这是我的AnimationViewController

    #import "AnimationViewController.h"
    #import "FlipView.h"
    #import "AnimationDelegate.h"

    @implementation AnimationViewController

    @synthesize flipView2;
    @synthesize panRecognizer;
    @synthesize panRegion;
    @synthesize imageBluehound,imageDoggie,imagePointy,imagePurpGuy,imageRedDog,imageWoof;

    - (id)init
    {
        self = [super init];
        if (self) {
            // Custom initialization
            step = 0;
        }
        return self;
    }

    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];

        // Release any cached data, images, etc that aren't in use.
    }

    #pragma mark - View lifecycle

    /*
    // Implement loadView to create a view hierarchy programmatically, without using a nib.
    - (void)loadView
    {
    }
    */

    - (void)dealloc
    {
        [flipView2 release];
        [panRecognizer release];
        [panRegion release];

        [super dealloc];
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        self.view.backgroundColor = [UIColor whiteColor];

        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onBackButtonPressed:)];

        // memorisation des imagesx
        imageBluehound = [UIImage imageNamed:@"Bluehound.gif"];
        imageDoggie = [UIImage imageNamed:@"Doggie.gif"];
        imagePointy = [UIImage imageNamed:@"Pointy.gif"];
        imagePurpGuy = [UIImage imageNamed:@"PurpGuy.gif"];
        imageRedDog = [UIImage imageNamed:@"RedDog.gif"];
        imageWoof = [UIImage imageNamed:@"Woof.gif"];


        animationDelegate2 = [[AnimationDelegate alloc] initWithSequenceType:kSequenceControlled
                                                               directionType:kDirectionForward];
        animationDelegate2.controller = self;
        animationDelegate2.perspectiveDepth = 1000;

        self.flipView2 = [[FlipView alloc] initWithAnimationType:kAnimationFlipHorizontal
                                                           frame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        animationDelegate2.transformView = flipView2;

        [self.view addSubview:flipView2];


        [flipView2 printText:@"" usingImage:imageBluehound backgroundColor:[UIColor greenColor] textColor:nil];
        [flipView2 printText:@"" usingImage:imageDoggie backgroundColor:[UIColor greenColor] textColor:nil];
        [flipView2 printText:@"" usingImage:imagePointy backgroundColor:[UIColor greenColor] textColor:nil];
        [flipView2 printText:@"" usingImage:imagePurpGuy backgroundColor:[UIColor greenColor] textColor:nil];
        [flipView2 printText:@"" usingImage:imageRedDog backgroundColor:[UIColor greenColor] textColor:nil];
        [flipView2 printText:@"" usingImage:imageWoof backgroundColor:[UIColor greenColor] textColor:nil];


        self.panRegion = [[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        [self.view addSubview:panRegion];

        self.panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panned:)];
        panRecognizer.delegate = self;
        panRecognizer.maximumNumberOfTouches = 1;
        panRecognizer.minimumNumberOfTouches = 1;
        [self.view addGestureRecognizer:panRecognizer];

    }

    - (void)onBackButtonPressed:(UIBarButtonItem *)sender
    {
        [self dismissModalViewControllerAnimated:YES];
    }

    - (void)panned:(UIPanGestureRecognizer *)recognizer
    {
        switch (recognizer.state) {
            case UIGestureRecognizerStatePossible:
                break;
    //        case UIGestureRecognizerStateRecognized: // for discrete recognizers
    //            break;
            case UIGestureRecognizerStateFailed: // cannot recognize for multi touch sequence
                break;
            case UIGestureRecognizerStateBegan: {
                // allow controlled flip only when touch begins within the pan region
                if (CGRectContainsPoint(panRegion.frame, [recognizer locationInView:self.view])) {
                    if (animationDelegate2.animationState == 0) {
                        [NSObject cancelPreviousPerformRequestsWithTarget:self];
                        animationDelegate2.sequenceType = kSequenceControlled;
                        animationDelegate2.animationLock = YES;
                    }
                }
            }
                break;
            case UIGestureRecognizerStateChanged: {
                if (animationDelegate2.animationLock) {
                    switch (flipView2.animationType) {
                        case kAnimationFlipVertical: {
                            float value = [recognizer translationInView:self.view].y;
                            [animationDelegate2 setTransformValue:value delegating:NO];
                        }
                            break;
                        case kAnimationFlipHorizontal: {
                            float value = [recognizer translationInView:self.view].x;
                            [animationDelegate2 setTransformValue:value delegating:NO];
                        }
                            break;
                        default:break;
                    }
                }
            }
                break;
            case UIGestureRecognizerStateCancelled: // cancellation touch
                break;
            case UIGestureRecognizerStateEnded: {
                if (animationDelegate2.animationLock) {
                    // provide inertia to panning gesture
                    float value = sqrtf(fabsf([recognizer velocityInView:self.view].x))/10.0f;
                    [animationDelegate2 endStateWithSpeed:value];
                }
            }
                break;
            default:
                break;
        }
    }

    // use this to trigger events after specific interactions
    - (void)animationDidFinish:(int)direction 
    {
        switch (step) {
            case 0:
                break;
            case 1:
                break;
            default:break;
        }
    }

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
    {
        if (UIInterfaceOrientationIsLandscape(orientation))
        {
            if (orientation == UIInterfaceOrientationLandscapeLeft)
            {
                NSLog(@"landscape left");
                self.flipView2.transform = CGAffineTransformMakeRotation(0);
            }
            else
            {
                NSLog(@"landscape right");
                self.flipView2.transform = CGAffineTransformMakeRotation(0);
            }
        }
    }


    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return YES;
    }

@end

这里是.h

#import <UIKit/UIKit.h>

@class FlipView;
@class AnimationDelegate;

@interface AnimationViewController : UIViewController <UIGestureRecognizerDelegate> {

    // use this to choreograph a sequence of animations that you want the user to step through
    int step;

    //the controller needs a reference to the delegate for control of the animation sequence
    AnimationDelegate *animationDelegate2;

    BOOL runWhenRestart;

}

//@property (nonatomic, retain) UIButton *boutonMenuGlissant;

@property (nonatomic, retain) UIImage *imageBluehound;
@property (nonatomic, retain) UIImage *imageDoggie;
@property (nonatomic, retain) UIImage *imagePointy;
@property (nonatomic, retain) UIImage *imagePurpGuy;
@property (nonatomic, retain) UIImage *imageRedDog;
@property (nonatomic, retain) UIImage *imageWoof;

@property (nonatomic, retain) FlipView *flipView2;
@property (nonatomic, retain) UIView *panRegion;
@property (nonatomic, retain) UIPanGestureRecognizer *panRecognizer;

- (void)onBackButtonPressed:(UIBarButtonItem *)sender;
- (void)panned:(UIPanGestureRecognizer *)recognizer;
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration;

// animation delegate will notify the controller when the animation frame has reached a position of rest
- (void)animationDidFinish:(int)direction;

@end

我在其他帖子中看到他们谈到了 autoSized 蒙版,但我做不到...

有什么想法或建议吗?谢谢!

【问题讨论】:

    标签: xcode ipad rotation autoresize


    【解决方案1】:

    尝试修改视图/图像的支柱和字符串。单击图像后,转到实用程序检查器的大小选项卡。 “自动调整大小”旁边的示例将显示视图在框架更改时的外观。打开/关闭红色连接器和箭头,以使图像在方向更改时看起来像您想要的那样。

    【讨论】:

    • 我不明白...我在哪里可以做到这一点?我没有.storyboard
    • 在您的 xib 文件中单击要修改的图像视图。然后按 Command-Option-5。这将调出尺寸检查器。在显示“自动调整大小”的框中,您可以切换红色箭头和连接器,以更改旋转设备时视图调整大小的方式。还要确保属性检查器(Command-Option-4)中的“模式”设置为图像的“缩放以填充”
    • 我没有尺寸检查器之类的东西...我想它需要有一个.storyboard
    • 看起来一切都在以编程方式完成。我不确定该怎么做,但可能会尝试添加一种方法,该方法会根据方向改变图像的大小。把它放在你的if (UIInterfaceOrientationIsLandscape(orientation))
    • 是的。这就是我最终所做的……这有点烦人,但它确实有效。我已经把我的代码放在willRotateToInterfaceOrientation
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    相关资源
    最近更新 更多