UIScrollViewDelegate not scrolling animated - iphone

I have created a subclass of UIScrollView that uses the UIScrollViewDelegate. I created it to work with OpenGL ES objects. I have the following code:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
for(InputEntity * iEnt in [sceneInstance inputObjects]){
iEnt.display.translation = GLKVector2Make(0, self.contentOffset.y);
}
}
It eventually puts all the content in the correct spot, but it does not update the locations until after the scroll view has stopped moving. Is there a way to fix this so it looks animated like UIScrollViews do?

do you have render method like setNeedDisplay:
if you have,you can:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
for(InputEntity * iEnt in [sceneInstance inputObjects]){
iEnt.display.translation = GLKVector2Make(0, self.contentOffset.y);
//render here
}
}

Related

UIScrollView menu and parallax effect

I need to create a main menu for my app with a UIScrollView. I have some images inside it that can be clicked. When I scroll the UIScrollView I need that on the background there are other two views that move creating a parallax effect.
Can someone provide me a sample code? I'm trying to work with
-(void) scrollViewDidScroll:(UIScrollView *)scrollView
but I cannot find any productive example about applying on my project.
How's this? The imageView will scroll up half as fast as the UIScrollView.
float y = scrollView.contentOffset.y;
CGRect imageFrame = self.imageView.frame;
imageFrame.origin.y = y/2;
self.imageView.frame = imageFrame;
This GitHub repository has an amazing implementation that works quite well:
https://github.com/ralfbernert/RBParallaxScrolling
Here's my test of the code, using a UIScrollView with pagination (in the front) and an image in the background:
http://clrk.it/211o3h0A053m
The bit of code that does this parallax trick works as follows:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == _scrollView) {
float speedFactor = _headerImageScrollView.contentSize.width / _scrollView.contentSize.width;
[_headerImageScrollView setContentOffset:CGPointMake(speedFactor * _scrollView.contentOffset.x, 0)];
}
}
In this code, I've got a UIScrollView that contains a UIImageView; I call this _headerImageScrollView.
In front of it, I have a second UIScrollView with pagination and the three labels. That one's called _scrollView.

Attempting to create a UIScrollView inside of a UIScrollView, like photos app

I'm having difficulty though. As soon as i make the content size of the outer scrollview be bigger than it's frame, the image that i'm zooming on it's subview (also a scrollview) is behaving weirdly.
Anybody have any ideas on how to do this? I'm making my own because i have problems with various ones on the internet and i also want it to support video, so i'm making it myself.
FOR ZOOMING inner scrollview you have to right this method.
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
if(scrollView!=Innerscr){
UIImageView *v=(UIImageView*)[scrollView viewWithTag:kTagImageViewInScrollView];
return v;
} else {
return nil;
}
}
and for scrolling images you have to write code in under method
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if(scrollView==Outerscr){
}
}

(iphone) can i make UIScrollView to recognize horizontal dragging only?

I'm using UIScrollView's canceling touch ability with canCancelContentTouches.
However, I 'd like the uiscrollview to attempt to cancel touch when it detected horizontal dragging(not vertical).
(Hope solution would be available under < iOS 3.13)
Thank you
Implement the UIScrollViewDelegate and then use something like this:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[scrollView setContentOffset: CGPointMake(0, scrollView.contentOffset.y)];
}
Another way would be having a UIScrollView which is smaller or equal to the size of its parent view and with a disabled "Always bounce horizontal".
The safest and most successful method I've found to constrain the movement of a scroll view is to subclass UIScrollView and override setContentOffset:animated: and setContentOffset: methods (code below).
The advantage of overriding these methods is that it directly alters the requested contentOffset before any of the UIKit code starts to act on it, avoiding any of the side effects that can occur when modifying the contentOffset in scrollViewDidScroll: or other UIScrollViewDelegate methods.
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated {
// restrict movement to horizontal only
CGPoint newOffset = CGPointMake(contentOffset.x, 0);
[super setContentOffset:newOffset animated:animated];
}
- (void)setContentOffset:(CGPoint)contentOffset {
// restrict movement to horizontal only
CGPoint newOffset = CGPointMake(contentOffset.x, 0);
[super setContentOffset:newOffset];
}
i guess i would use the method scrollViewWillBeginDragging
found in the UIScrollViewDelegate
and inside i could control if user is going horizontally or not...
scrollViewWillBeginDragging:
Tells the delegate when the scroll view is about to start scrolling the content.
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
Parameters
scrollView
The scroll-view object that is about to scroll the content view.
Discussion
The delegate might not receive this message until dragging has occurred over a small distance.
Availability
Available in iOS 2.0 and later.

How do I scroll a set of UIScrollViews together?

Setup: I have a UITableView, each UITableViewCell has a UIScrollView. What I am trying to do is to get all of the UIScrollViews to scroll together, such that when you scroll one of them all of the UIScrollViews appear to scroll simultaneously.
What I've done is subclass UITableView so that it has an array of all of the UIScrollViews within its table cells. I then forwarded TouchesBegan, TouchesMoved, TouchesCancelled, and TouchesEnded from the UITableView to all of the UIScrollViews in the array.
This doesn't appear to work. The UIScrollViews do not scroll! The only way I've managed to get this to work is to call the setContentOffset: method on the scrollviews. However, this is a pretty bad solution since it doesn't give you the swiping and deceleration features of the UIScrollView.
Any ideas on why my touches methods aren't getting to the UIScrollViews? Or a better way to implement this?
Ok, got it working. Thanks for the tips Ricki!
2 things to add to Ricki's solution, if you want to avoid an infinite loop, you have to check to see whether the scrollView's tracking or dragged properties are set. This will insure that only the ScrollView that is actually being dragged is calling the delegate.
- (void)scrollViewDidScroll:(UIScrollView *) theScrollView {
if (theScrollView.dragging || theScrollView.tracking)
[self.delegate scrolling:[theScrollView contentOffSet]];
}
Also, in the scrolling method of the delegate, I set animated to NO, this got rid of the delay between the initial swipe and the other scrollviews getting updated.
I did something "similar" where I had 4 scrollViews incased inside a parent view.
I placed a scrollView inside a UIView, this UIView was passed a delegate from its parentView, that was the view who kept track of all the scrollViews. The UIView containing a scrollVIew implemented the UIScrollViewDelegate and this method;
- (void)scrollViewDidScroll:(UIScrollView *) theScrollView {
[self.delegate scrolling:[self.scrollView contentOffSet]];
}
Now the parent view did this on all the scrollViews:
- (void) scrolling:(CGFloat) offset {
for(UIScrollView *s in self) {
[s setContentOffset:offset animated:YES];
}
}
It is of course a bit of a strain on the CPU, but scrolling several views will be that under any circumstances :/
Hope this was something in the direction of what you needed, and that it made any sense.
Added:
I took me 8 different paths and a lot of mass chaos before I made it work. I dropped the touchedBegan approach early, there is just no way to write something that comes close to Apples swipe, flick, scroll algorithms.
I don't know if the tableview and scrollview will "steal" each others touch events, but as I can read from your description you made that part work.
A follow up idea to ease the CPU usage. add each scrollview to a cell, set its tag=14, now when scrolling asked for all visible cells only, ask for viewWithTag=14, set the contentOffset on this. Save the content offset globally so you can assign it to cells being scrolled onto the screen in cellForRowAtIndexPath.
So set the offSet to a global property, in cellForRowAtIndexPath find the view with tag = 14, set its offset. This way you don't even need a reference to the scrollViews only the delegate.
If you have differently sized UIScrollViews and are using paging, this works great:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView {
#pragma unused(_scrollView)
categoryPageControlIsChangingPage = NO;
for (UIImageView *iv in [categoryScrollView subviews]) {
iv.alpha = (iv.tag != categoryPageControl.currentPage+1)?0.5f:1.0f;
ILogPlus(#"%i %i", iv.tag, categoryPageControl.currentPage+1);
}
[self scrolling:_scrollView];
}
- (void)scrolling:(UIScrollView *)sv {
CGFloat offsetX = sv.contentOffset.x;
CGFloat ratio = offsetX/sv.contentSize.width;
if ([sv isEqual:categoryScrollView]) {
[categoryScrollViewLarge setContentOffset:CGPointMake(ratio*categoryScrollViewLarge.contentSize.width, 0) animated:YES];
}else {
[categoryScrollView setContentOffset:CGPointMake(ratio*categoryScrollView.contentSize.width, 0) animated:YES];
}
}

Is there a way to create a scrollViewISZooming method similar to the scrollViewDidEndZooming?

I am looking for a way to create a scrollViewIsZooming method that is called while zooming is occuring. Does anyone know of a way to do this?
I am wanting to use it to keep the content centered in the scrollView while zooming. If I use the scrollViewDidEndZooming method, the content snaps back to the center after zooming is finished.
Thanks!
There's a zooming propriety on UIScrollView :
#property(nonatomic, readonly, getter=isZooming) BOOL zooming
If you check it in each
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
call it should work.
What about viewForZoomingInScrollView:
If you put a pinch gesture recogniser in the view then connect it to an action you can control zooming that way. I have an action "doPinch" and an outlet "pinchRecognizer" that I use like this:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.zoomableImage;
}
Then
- (IBAction)doPinch:(id)sender
{
NSLog(#"In the pinch action now with scale: %f", self.pinchRecognizer.scale);
[scrollView setZoomScale:self.pinchRecognizer.scale animated:NO];
}
Might be easier. Note that scrollView is my outlet connected to the scroll view.