4

I want to achieve a rather simple effect. A scrollView and a static button above it. So far not problem. Unfortunately a problem occurs if I want to scroll the view "trough" the button. So start dragging inside the button will effect the button, not the scroll view.

Any ideas on how to achieve that?

Thanks a lot!

I think this approach won't work because touchesBegan is deprecated in UIScrollView … (Is this info true?)

Not the solution :-)

@implementation MySuperButton

+ (id)buttonWithFrame:(CGRect)frame {
    return [[[self alloc] initWithFrame:frame] autorelease];
}

- (id)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        NSLog(@"initWithFrame");
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self.nextResponder touchesBegan:touches withEvent:event];
    NSLog(@"[self nextResponder]: %@", [self nextResponder]);
}

@end

Unfotunately this does not work, yet. I think the next responder is a my UIView not my UIScrollView. Any ideas?

If my viewcontroller which is the nextResponder, passes the touches to the scrollView, nothing happens aswell.

danielreiser
  • 5,292
  • 5
  • 31
  • 43

5 Answers5

12

Add the button not to the scroll view but to its parent view, and everything should work just fine. Touches on the button directly will get delivered to the button, and touches on the background (scroll view) will get delivered to the scroll view.

As the button isn't a subview of the scroll view it won't move with the background (which I suppose you mean with static).

Eiko
  • 25,601
  • 15
  • 56
  • 71
  • 1
    Does this solve your problem or did I misread your question? What should happen in case of touching the button (scroll or not)? – Eiko Apr 04 '11 at 18:25
  • +1 This is correct if he doesn't want it to scroll, but maybe he wants it to. I don't understand why he still haven't specified which behavior he is after. – Erik B Apr 10 '11 at 21:39
1

I never played alot with the touch events and such, but i'll try to help with my basic understanding anyway.

Try setting the button's exclusiveTouch to NO. Not sure, but maybe enableMultitouch to YES as well.

If that doesn't work i'd try maybe subclassing UIButton, overriding the touch events and passing them to [self nextResponder] before/after [super touchEventX:]?

Zaky German
  • 14,324
  • 4
  • 25
  • 31
  • Thanks for your help. This sounds kind of complicated to me, as I never played a lot with touch events, too :-) I'll give it a try. – danielreiser Apr 04 '11 at 15:57
  • @resign i'd try also doing the same with touchesEnded and touchesMoved. also did setting exclusiveTouch to NO change anything? – Zaky German Apr 04 '11 at 20:10
  • @rdesign try scrollView.delaysContentTouches = YES; scrollView.canCancelContentTouches = YES; Here's a question that was asked here about the same thing: http://stackoverflow.com/questions/650437/iphone-uiscrollview-with-uibuttons-how-to-recreate-springboard – Zaky German Apr 05 '11 at 15:17
1

It's kinda hard to understand what you're trying to achieve. Do you want to scroll through the button or not?

So start dragging inside the button will effect the button, not the scroll view.

It's not clear if that's the behavior you want or the behavior you're trying to change.

If it's the former Eiko's answer is correct and if it's the later you should add the button as a subview of the scroll view and update its frame whenever scrollViewDidScroll: is called. I've never tried this, but it works in theory.

Erik B
  • 40,889
  • 25
  • 119
  • 135
0

Move the whole Scroll View down by the height of your Button and shrink it's height by the same amount. Then put your Button above the the Scroll View - so it's not a child of the Scroll View.

Wex
  • 4,434
  • 3
  • 33
  • 47
  • Thanks for your answer. Unfortunately I described it the wrong way. I meant on top of it. So the button should be on top of the scrollview. The scrollview should still recognize the touches, the button receives. The button should only fire on touchdowninside. – danielreiser Mar 31 '11 at 15:20
  • 1
    So you want one touch to effect two different objects? That sounds like a bad interface idea... what's the reason for the button? – Wex Mar 31 '11 at 16:05
  • I want to make a menu. something like there's a bottle of wine(the button). you can slide the background to choose wheather you want to find a location inside or outside to drink it (get the idea?) – danielreiser Mar 31 '11 at 23:36
0

If you add simple button in a view then if u touch on button. it will calls Action of it.

Sameway

If you add simple button in a ScroolView then if u touch on button. it will calls Action of it.

And if u touch on the empty area of scroolview then the delegate methods are called of UIScrollView.

If you put your finger on button and Drag it will obviosly effect on the scrollview. scrollview will drag 100%

Rakesh Bhatt
  • 4,606
  • 3
  • 25
  • 38