简体   繁体   English

iPhone旋转时更改单元格中的背景图像

[英]change background image in a cell when iPhone rotates

My tableview has 4 cells, and each cells has one different picture as background. 我的tableview有4个单元格,每个单元格都有一张不同的图片作为背景。

When I rotate the iPhone, the height of the rows change, and also the pictures. 当我旋转iPhone时,行的高度和图片也会改变。

Is really easy to change picture if the position is in landscape or in portrait, but the picture change when the iPhone is already rotated, not while is rotating! 如果位置是横向或纵向,更改图片确实很容易,但是当iPhone已经旋转时(而不是旋转时),图片就会改变! What's the problem? 有什么问题? For example: the iPhone is in portrait and the user rotates it in landscape. 例如:iPhone是纵向的,用户可以横向旋转。 That there is a very ugly effect, because the pictures change just when the iPhone is in landscape, and for a fraction of a second the user sees the portrait picture deformed (before that it changes). 这有一个非常丑陋的效果,因为图片恰好在iPhone处于横向时改变,并且在不到一秒钟的时间内,用户会看到肖像图片变形(在它改变之前)。

How can I solve it? 我该如何解决?

Thanks 谢谢

The easy way: 简单的方法:
Implement willAnimateRotationToInterfaceOrientation in your view controller. 在您的视图控制器中实现willAnimateRotationToInterfaceOrientation Inside this method you can reorient your pictures and they'll automatically be animated. 在此方法内,您可以重新调整图片的方向,然后它们将自动动画化。 There's a short overview this and other available interface orientation methods at http://www.dizzey.com/development/ios/handling-layout-on-uiinterfaceorientation-change/ http://www.dizzey.com/development/ios/handling-layout-on-uiinterfaceorientation-change/上有此方法和其他可用的界面定向方法的简短概述

Pointlessly difficult way: 无意义的困难方式:
You could manually track acceleration and write your own animation blocks. 您可以手动跟踪加速度并编写自己的动画块。 Set your view controller as a UIAccelerometerDelegate. 将视图控制器设置为UIAccelerometerDelegate。 Reference the sharedAccelerometer as follows. 如下参考sharedAccelerometer。

    accel = [UIAccelerometer sharedAccelerometer];
    accel.delegate = self;
    accel.updateInterval = 1.0f/60.0f;

Then implement this delegate method. 然后实现此委托方法。 This will be run every time the accelerometer notices acceleration. 每当加速度计注意到加速度时,将运行该程序。 Tracking acceleration in the X axis will give you landscape/portrait orientation. 在X轴上跟踪加速度将为您提供横向/纵向方向。

#pragma mark UIAccelerometer delegate method
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    if(acceleration.x > 0.8) //landscape right
    {
        [UIView beginAnimations:@"text spin" context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationDuration:0.2];
        //do some animation to landscape right
        [UIView commitAnimations];
    }
    if(acceleration.x < -0.8) //landscape left
    {
        [UIView beginAnimations:@"text spin" context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationDuration:0.2];      
        //do some animation to landscape left
        [UIView commitAnimations];
    }
    if(acceleration.x < 0.2 && acceleration.x > -0.2) //portrait 
    {
        [UIView beginAnimations:@"text spin" context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationDuration:0.2];
        //do some animation to portrait view
        [UIView commitAnimations];
    }

In a situation like this, during a rotation I tend to just use the .hidden property on elements (myObject.hidden = YES) and then restore them when the rotation is complete. 在这种情况下,在旋转期间,我倾向于只对元素使用.hidden属性(myObject.hidden = YES),然后在旋转完成后恢复它们。

I imagine your picture frames have more room in landscape view - another suggestion if this is true, is to keep them the same size in both orientations, then you wouldn't need to hide them during a rotation, for example. 我想象您的相框在横向视图中有更多的空间-如果这是真的,另一个建议是在两个方向上将它们保持相同的大小,那么您就不需要在旋转过程中隐藏它们。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM