views:

1177

answers:

2

Hi,

I have a UIView inside my xib in IB and inside that is a UIScrollview that is a small 80x80 square and dynamically loaded with 8 or so 80 x 80 thumbnail images.

The UIScrollview is not clipping the images so that they extend out either side so you can swipe left and right to scroll a chosen image into the the centre, paging is on so they snap ti each image.

I have researched and found this is the best and possibly only way to do this.

The UIScrollview sits in a 'container' UIView for one reason, it is there to receive the touches/swipes and pass them down to it's child the UIScrollview as otherwise all touches would have to start in the small 80x80 UIScrollview area and I wan them to be anywhere along the row of images.

I have seen some sample code somewhere for doing this but just can not implement it.

Treat me as a noob, starting from beginning to end, how should the UIView and UIScrollview be set up in IB to allow any touches to be passed, and what code should I put into where?

The UIView is set up as scroll_container and the child UIScrollview is char_scroll

At the moment I have got it all working except for the touches being passed from the parent to the child, and at the moment the touches have to always start inside the UIScrollview (tiny 80x80 box in centre) when I want to be able to swipe left or right in the long 480X80 horizontal parent UIView and have this still scroll the child UIScrollview.

Hope you can help and understand what I mean!

A: 

All my instrincts say this is not the way to do this!

Even if:

(a) the Apple docs say that this works and how to do it;

(b) and you can get the hitTest thingummy working and get the UIEvent objects flowing nicely

if it was me I would expect the first change Apple make to the OS to break the code!

What I would do is:

  • Use the Core Animation API.
  • Model the deceleration of the images when a moving finger lets go of them yourself
  • Try different rates of deceleration and different snapping algorithms (try with and without bouncing) until you find an acceptable user experience (try prototyping in DHTML before going into Cocoa Touch)
  • Stop using UIScrollView and use simply a UIView

You need to model in your state:

state1: the (delta x,delta y) position change requested by user on this touch

OR

state2: the (delta x,delta y) and (veclocity x, velocity y) and clock() time of the finger when released if images are still slowing down and not come to rest

state3: images stationary

From this the animation parameters can be calculated:

Initial: delta-x, delta-y, velocity-x, velocity-y, clock() time in ms

If supporting bouncing: any intervening turning points

Final: delta-x, delta-y, velocity-x, velocity-y, clock() time in ms

And from these animation parameters, a set of animation key frames can be calculated and fed to Core Animation. (A large number of key frames are necessary to smoothly show a custom deceleration during the animation.)

--

What are the references you have found to say what you suggest is the best way?

martinr
A: 

You could subclass UIScrollView and override the -pointInside:withEvent: method. Default implementation of UIView returns something like CGRectContainsPoint(self.bounds, thePointToTest);. Just test over a wider rect.

Visa