UIScrollView sporadically loses bounce and fluid scrolling - iphone

I have successfully implemented a working UIScrollView layer on top of Cocos2d using this fantastic tutorial:
http://bitbattalion.com/2011/09/uikit-uiscrollview-and-cocos2d/
However, the scroll view sporadically becomes sluggish and choppy, including almost always upon loading the scene. When it happens, the scrolling and is no longer is able to be flicked up or down with any sort of variable velocity. It also loses all of it's bounce and just stay partially of screen until the next touch is registered.
The weird thing is though, that if I simply pull down my Notification Center, it fixes the scroll view every time. The scrolling all of a sudden becomes perfectly fluid and the edges bounce beautifully into place.
What is happening here?

Instruments may help to tackle the problem. Run using the CoreAnimation Tool so you can get a good handle on where you might be experiencing the drop in frame rate. You'll need to have your device connected to run the CoreAnimation tool.
This tutorial is also available at http://mobileoverlord.com/instruments-optimizing-core-animation/
Select Profile from the build scheme.
Select the CoreAnimation Profiling Tool
When the tool is running scroll around in your scroll view and keep an eye on frame rate. 60 is great.
If you need to dial in on it any more you can turn on Color Blended Layers from the Debug Options in the Instruments Panel View(Menu) -> Instruments
Without Color Blended Layers (Left)
With Color Blended Layers (Right)
The basic rule of thumb here is, the more red you see the less optimized the view is for the animation. Although, you don't want to remove all red either, %50 is about right.

Related

How do I keep the title bar from causing rendering issues in Metal?

When I draw any shape (Whether it's textured or not) it will flicker when I go to fullscreen and make the title bar show when I move the mouse to the top. It will turn lighter and then back for like a split second. I don't know if this happens with solid colors, but when I color the vertices and draw a gradient or apply textures, this happens. It's annoying, even though it's subtle. How can I fix it?
How to reproduce: Create a shape in Metal, either color the vertices differently to create a gradient, or apply a texture to it, and make the window fullscreen. Move the cursor to the top of the window. When the title bar shows up, the shape/texture will flash briefly. How can I fix that?
I am using macOS 1.15 Catalina.
This can be demonstrated even with the Xcode's example Metal game. Just go to fullscreen, show the title bar, and it will be unsmooth when you show and unshow the titlebar.
UPDATE: I have realized, that if I put the drawing code in viewDidLoad() instead of draw() this behavior will cease. How do I make it so that it's not buggy without having to only render ONE frame?
ANOTHER UPDATE: It does occur for solid colors.
This is a very difficult question, because I don't know what to try. This bug is also very hard to fix, because it only sometimes happens.
Example of the bug (Video)
Another example of the bug that displays the "white flicker" (Video)
Also this question was only half answered, because it turns out that I managed to fix the problem of the titlebar blocking the framerate, but there is still a problem with the white flash.
After watching your video, I understand what your problem is. In your case UI (Tittle Bar) is blocking the main thread, it's normal behavior because MTKView rendering occurs on the main application threads. You need to create a custom metal view and implement a render loop on a background thread.
Your best bet is to report this as a bug to Apple. I tried to research and there seems to be no results about this bug.
I assume you want a fix, but all I got is a workaround:
It doesn't seem to occur while the drawing code only runs once, in viewDidLoad. Maybe you could keep it there, and manually call the MetalView's draw method 60 times a second. I figured out how to get what I said to work. Leave the drawing code in draw, but make sure to set the preferred framerate to 0. Then manually redraw it 60 times a second. Because the draw method auto redrawing is buggy in this sense.

endless vertical scrolling background

I want to make an endless vertical scrolling layer that gives the impression that the main character is moving upwards. I have been brainstorming on how to achieve this.
My issue is that I want objects to appear as if they are coming from above and below the screen at the same time. Secondly, I want to be able to move the main character to create and destroy box2d joints between it and some of the objects appearing on the screen. What is the best way to achieve this with consuming too much memory? I would appreciate any help on this.
Apple did a wonderful tutorial of this in a WWDC 2011 video session. It was "UITableView Changes, Tips & Tricks" and it's about 35m40sec into the video.
Since the use of the UITableView is really just a UIScrollView for the purposes of the background, you could just use a UIScrollView and you can either have it move on timer or events as needed.
Think of your player as moving within a stationary bounding box. The background can scroll using the aforementioned pooling method (as the background tile scrolls off the screen it is placed into a pool, and before a new tile is instantiated the pool is checked for available reusable tiles). Thirdly, your enemy objects will simply approach from either the bottom of the screen or the top.
Imagine your idea without the scrolling background (flying effect) and you should find that the problem is relatively straightforward.
I also needed and endless scrolling background layer. This can do exactly that, and it is super simple to set up and use. Just copy the four files in to the cocos2d folder in your project, then follow the quick tutorial seen on the github. Make sure the image you use is seamless (when you line them up vertically you can't tell where one ends.

scrolling becomes choppy after device rotation

I have a UICollectionView with some custom cells, it uses the flow layout and it has 3 columns on landscape and 2 columns on portrait. When I started the app on landscape, scrolling gives me 55-60 FPS, which is fairly good. However, after I rotate for some reason it drops down to 30 FPS and under. Any idea how rotation could destroy FPS in scrolling?
I've heard some talk in WWDC video's that rasterization might be causing it. Is it true that I'd have to turn off and on back rasterization on rotation?
Are you running any other animations while scrolling? These will affect performance.
Are you using the CALayer shadow property on any elements? You may be better off using an shadow image instead.
Do your interface elements (or anything moving, for that matter) contain transparency? Check this by using the simulator and selecting Debug - Color Blended Layers.
Blending slows down performance a great deal, so use opaque views wherever you can.

Can it be done using UIAnimation?

I am developing a game for iPhone and I came across a question whether this can be done using UIAnimation or I have to switch to OpenGL.
If you see the picture above, you can realize there is a rectangle (it will have background image) having many holes in it. User is supposed to touch on holes and initiate the animation. The holes are actually on the image and are not some view. Do you know any article or tutorial I need to read in order to understand this can be done using UIAnimation?
It's more like chess. When you tap a hole, there is a view placed and moved at z-axis and then when you press the other hole, the view is moved to that hole and so on.
I believe it can be done with UIViews too. The holes can be made by using an image with "transparent" holes in it. You can place this UIView on top of a background image(if required). When touches are detected, you can animate another view on top of this view using addSubview:. You can keep this other view outside the screen bounds initially so that the animation seems like a sliding animation.
yes UIView is also an option in your case. if you feel that you will use high quality lots of graphic and will do lots of animation stuff at once on the screen, openGL will be the best or perhaps the gane you are making is a 2d game so you must consider COCOS2D "http://www.cocos2d-iphone.org/" for kick starting your game you'll surely get many effects and other glittering stuff with it. and bes of all it is openGL based..!! Performance will be too high for you..!!
Best of luck..!!

Tinting iPhone application screen red

I'm trying to place a red tint on all the screens of my iPhone application. I've experimented on a bitmap and found I get the effect I want by compositing a dark red color onto the screen image using Multiply (kCGBlendModeMultiply).
So the question is how to efficiently do this in real time on the iPhone?
One dumb way might be to grab a bitmap of the current screen, composite into the bitmap and then write the composited bitmap back to the screen. This seems like it would almost certainly be too slow. In addition, I need some way of knowing when part of the screen has been redrawn so I can update the tinting.
I can almost get the effect I want by putting a red, translucent, fullscreen UIView above everything. That tints everything red within further intervention on my part, but the effect is much "muddier" than results from the composite.
So do any wizards out there know of some mechanism I can use to automatically composite the red over the app in similar fashion to what the translucent red UIView does?
I managed to somewhat make this work but with some side-effects:
I setup a UIView on top of all my app-views (attached to the window) which is not userInteractionEnabled and which is opaque
This UIView carries some custom drawRect-method which first fills the complete area with red color and then after having made a "screenshot" of my window-viewhierarchy I am rendering this image with
CGContextSetBlendMode( c, kCGBlendModeMultiply);
to the UIView.
To constantly update this UIView to the current state of the apps UIViews I constantly produce "screenshots" and render them as fast as possible.
I setup an NSTimer which is doing this snapshotting/rendering in a defined frequency and which is added to the the NSRunLoop for "Tracking".
RESULT: some really laggy response from the UI with several fancy effects, but still usable though if you do not set the frequency of snapshotting/rendering to high.
See screenshot here...
The result looks okay, but the usability really suffers a lot. I had a look at the OpenGL-examples before trying this aproach, but OpenGL is a whole lot of different (mostly C) code which seems to be very near to the hardware and which gives you a real headache.
So, the described approach is what I will shoot for with my next app. I hope Apple accepts it even though it degrades UXP during nightvision mode. They should simply make CALayer filter-backed then my problem will definitely be solved a whole lot better and performing nicely.
You could try this: subclass UIView. Add code to -drawRect method to draw the overlay. Make your UIView subclass pose as UIView everywhere in your app with
class_poseAs ([CustomUIView class], [UIView class]);