Такое поведение обычно проявляется, когда у вас нет поддержки readline
. Если вы используете MacPorts, попробуйте port install readline
, посмотрите, исправит ли это. Вы также можете увидеть на этой странице для некоторых дальнейших объяснений.
(Также полезно знать: некоторые программы не используют readline
, даже если они присутствуют в системе. Вы можете принудительно включить их с помощью rlwrap
(port install rlwrap
). Например: rlwrap ocaml -init code.ml
запустится OCaml, прочитайте code.ml и запустите REPL с поддержкой readline)
Try making your scrollview less than the size of the screen (width-wise), but uncheck the "Clip Subviews" checkbox in IB. Then, overlay a transparent, userInteractionEnabled = NO view on top of it (at full width), which overrides hitTest:withEvent: to return your scroll view. That should give you what you're looking for. See this answer for more details.
Take a look at the -scrollView:didEndDragging:willDecelerate:
method on UIScrollViewDelegate
. Something like:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
int x = scrollView.contentOffset.x;
int xOff = x % 50;
if(xOff < 25)
x -= xOff;
else
x += 50 - xOff;
int halfW = scrollView.contentSize.width / 2; // the width of the whole content view, not just the scroll view
if(x > halfW)
x = halfW;
[scrollView setContentOffset:CGPointMake(x,scrollView.contentOffset.y)];
}
It isn't perfect—last I tried this code I got some ugly behavior (jumping, as I recall) when returning from a rubber-banded scroll. You might be able to avoid that by simply setting the scroll view's bounces
property to NO
.
Поскольку мне пока не разрешено комментировать, я добавлю здесь свои комментарии к ответу Ноя.
Я успешно добился этого с помощью метода, описанного Ноа Уизерспун. Я работал над поведением прыжка, просто не вызывая метод setContentOffset:
, когда scrollview выходит за границы.
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
// Don't snap when at the edges because that will override the bounce mechanic
if (self.contentOffset.x < 0 || self.contentOffset.x + self.bounds.size.width > self.contentSize.width)
return;
...
}
Я также обнаружил, что мне нужно реализовать метод -scrollViewWillBeginDecelerating:
в UIScrollViewDelegate
, чтобы уловить все случаи.
Существует также другое решение, которое, вероятно, немного лучше, чем наложение прокрутки на другое представление и переопределение hitTest.
Вы можете создать подкласс UIScrollView и переопределить его pointInside. Затем просмотр прокрутки может реагировать на прикосновения за пределами его рамки. В остальном конечно же.
@interface PagingScrollView : UIScrollView {
UIEdgeInsets responseInsets;
}
@property (nonatomic, assign) UIEdgeInsets responseInsets;
@end
@implementation PagingScrollView
@synthesize responseInsets;
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
CGPoint parentLocation = [self convertPoint:point toView:[self superview]];
CGRect responseRect = self.frame;
responseRect.origin.x -= responseInsets.left;
responseRect.origin.y -= responseInsets.top;
responseRect.size.width += (responseInsets.left + responseInsets.right);
responseRect.size.height += (responseInsets.top + responseInsets.bottom);
return CGRectContainsPoint(responseRect, parentLocation);
}
@end