Weird UIView transforms in Retina iPhone

I’m having a problem I don’t understand. I’m developing an OpenGL app for iOS. Because at some points I want to force the orientation of the view programatically, and Apple for whatever reason doesn’t make it easy (or even possible), I’m doing it by hand.

I return always NO in shouldAutoRotateToInterfaceOrientation, and when I want to change the orientation (to portrait, for example), I do something like this in the UIView:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[self setTransform:CGAffineTransformMake(1, 0, 0, 1, 0, 0)];
[self setBounds:CGRectMake(0, 0, 768, 1024)];
</code>
<code>[self setTransform:CGAffineTransformMake(1, 0, 0, 1, 0, 0)]; [self setBounds:CGRectMake(0, 0, 768, 1024)]; </code>
[self setTransform:CGAffineTransformMake(1, 0, 0, 1, 0, 0)];
[self setBounds:CGRectMake(0, 0, 768, 1024)];

This works fine.

In order to support Retina devices, I started checking [UIScreen mainScreen].scale, and setting self.contentScaleFactor accordingly. I also modified the code above to account for the new dimensions, like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[self setTransform:CGAffineTransformMake(1, 0, 0, 1, 0, 0)];
[self setBounds:CGRectMake(0, 0, 2*768, 2*1024)];
</code>
<code>[self setTransform:CGAffineTransformMake(1, 0, 0, 1, 0, 0)]; [self setBounds:CGRectMake(0, 0, 2*768, 2*1024)]; </code>
[self setTransform:CGAffineTransformMake(1, 0, 0, 1, 0, 0)];
[self setBounds:CGRectMake(0, 0, 2*768, 2*1024)];

Same rotation, different size. The weird result with this is that I get a “screen” with the right size, but offsetted half a screen to the bottom and the left. To correct for this, I need to do the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[self setTransform:CGAffineTransformMake(1, 0, 0, 1, 0, 0)];
[self setBounds:CGRectMake(-768, -1024, 2*768 - 768, 2*1024 - 1024)];
</code>
<code>[self setTransform:CGAffineTransformMake(1, 0, 0, 1, 0, 0)]; [self setBounds:CGRectMake(-768, -1024, 2*768 - 768, 2*1024 - 1024)]; </code>
[self setTransform:CGAffineTransformMake(1, 0, 0, 1, 0, 0)];
[self setBounds:CGRectMake(-768, -1024, 2*768 - 768, 2*1024 - 1024)];

This works, but it’s ugly, I also need to make similar corrections when I get touch coordinates, and worst of all, I don’t understand what’s going on or why the above “correction” works.

Can anyone shed some light on this issue?

Don’t hard-code screen size values!

If you get screen size values from [[UIScreen mainScreen] bounds]:

  • Take the status bar height into account.
  • If portrait: set width to screen height and height to screen width

    — Look at my code to see what I’m talking about: UIScreen+Flexible category.

If you get screen size values from the parent View:

  • Make sure you aren’t setting any frames before the parent view’s frame is set.

Also, Use setFrame. I can’t think of a case where you should be setting bounds?*

*I may be wrong about bound-setting.

In OpenGL ES all your transformation need to be make on OpenGL ES context.
And on your view layer you need to set scaleFactor on view object which is GLKView type.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>view.contentScaleFactor = [UIScreen mainScreen].scale;
</code>
<code>view.contentScaleFactor = [UIScreen mainScreen].scale; </code>
view.contentScaleFactor = [UIScreen mainScreen].scale;

And do not forget to set the glViewPort.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>glViewport(0,
0,
_screenWidth*self.view.contentScaleFactor,
_screenHeight*self.view.contentScaleFactor);
</code>
<code>glViewport(0, 0, _screenWidth*self.view.contentScaleFactor, _screenHeight*self.view.contentScaleFactor); </code>
glViewport(0,
           0,
           _screenWidth*self.view.contentScaleFactor,
           _screenHeight*self.view.contentScaleFactor);

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật