UIScrollView
UIScrollView
一.UIScrollView介绍
The UIScrollView
class provides support for displaying content that is larger than the size of the application’s window. It enables users to scroll within that content by making swiping gestures, and to zoom in and back from portions of the content by making pinching gestures.
UIScrollView类提供了支持显示的内容比应用程序的窗口的大小。它使用户能够滚动内容通过划动手势,和放大,从部分内容通过缩放手势。
UIScrollView
is the superclass of several UIKit classes including UITableView
and UITextView
.UIScrollView是几个UIKit类的超类包括UITableView和UITextView。
The central notion of a UIScrollView
object (or, simply, a scroll view) is that it is a view whose origin is adjustable over the content view. It clips the content to its frame, which generally (but not necessarily) coincides with that of the application’s main window. A scroll view tracks the movements of fingers and adjusts the origin accordingly. The view that is showing its content “through” the scroll view draws that portion of itself based on the new origin, which is pinned to an offset in the content view. The scroll view itself does no drawing except for displaying vertical and horizontal scroll indicators. The scroll view must know the size of the content view so it knows when to stop scrolling; by default, it “bounces” back when scrolling exceeds the bounds of the content.
UIScrollView对象的核心概念(或者简单,滚动视图),它是一个视图,其起源可调内容视图。它剪辑的内容框架,通常(但不一定)一致的应用程序的主窗口。一个滚动视图跟踪手指的运动,并相应地调整原点。视图显示其内容”通过“滚动视图本身吸引这部分基于新的来源,这是固定在一个偏移量在内容视图中。滚动视图本身并没有画除了显示垂直和水平滚动指标。滚动视图必须知道内容视图的大小所以它知道何时停止滚动;默认情况下,它“反弹”当滚动超过的范围内容。
The object that manages the drawing of content displayed in a scroll view should tile the content’s subviews so that no view exceeds the size of the screen. As users scroll in the scroll view, this object should add and remove subviews as necessary.
管理的对象的内容显示在滚动视图应该瓷砖内容的子视图,所以没有查看超过屏幕的大小。当用户滚动滚动视图,这个对象应该根据需要添加和删除子视图。
Because a scroll view has no scroll bars, it must know whether a touch signals an intent to scroll versus an intent to track a subview in the content. To make this determination, it temporarily intercepts a touch-down event by starting a timer and, before the timer fires, seeing if the touching finger makes any movement. If the timer fires without a significant change in position, the scroll view sends tracking events to the touched subview of the content view. If the user then drags their finger far enough before the timer elapses, the scroll view cancels any tracking in the subview and performs the scrolling itself. Subclasses can override the touchesShouldBegin:withEvent:inContentView:
, pagingEnabled
, and touchesShouldCancelInContentView:
methods (which are called by the scroll view) to affect how the scroll view handles scrolling gestures.
因为没有滚动条的滚动视图,它必须知道一个触摸信号一个滚动的意图和目的跟踪子视图的内容。使这一决心,暂时拦截代表事件启动一个计时器,计时器大火之前,看到如果触摸手指使任何运动。如果计时器火灾没有显著改变位置,滚动视图发送跟踪事件触动了内容视图的子视图。如果用户然后拖动手指足够远的计时器结束之前,滚动视图取消子视图中的任何跟踪和执行滚动本身。子类可以重写touchesShouldBegin:withEvent:inContentView:pagingEnabled touchesShouldCancelInContentView:方法(由滚动视图调用)影响滚动视图如何处理滚动姿态。
The UIScrollView
class can have a delegate that must adopt the UIScrollViewDelegate
protocol. For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView:
and scrollViewDidEndZooming:withView:atScale:
; in addition, the maximum (maximumZoomScale
) and minimum ( minimumZoomScale
) zoom scale must be different.
UIScrollView类可以有一个委托,必须采用UIScrollViewDelegate协议。缩放和移动工作,委托必须实现两viewForZoomingInScrollView:scrollViewDidEndZooming:withView:atScale:;此外,最大(maximumZoomScale)和最小(minimumZoomScale)缩放尺度必须是不同的。
For information about basic view behaviors, see View Programming Guide for iOS.
信息基本视图的行为,看到视图iOS编程指南。
二.Managing the Display of Content 管理内容的显示
- setContentOffset:animated:
Sets the offset from the content view’s origin that corresponds to the receiver’s origin.
设置偏移量从内容视图的起源相对应的接收器的起源。
Declaration
SWIFT
1 func setContentOffset(_ contentOffset: CGPoint, 2 animated animated: Bool)
OBJECTIVE-C
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
Parameters
contentOffset |
A point (expressed in points) that is offset from the content view’s origin. |
animated |
|
contentOffset |
一个点(点)表达抵消从内容视图的起源。 |
animated |
是的动画以恒定速度过渡到新的抵消,没有立即转变。 |
-
Availability
Available in iOS 2.0 and later.
See Also
-
contentOffset
PropertyThe point at which the origin of the content view is offset from the origin of the scroll view.
Discussion
The default value is
CGPointZero
.Availability
Available in iOS 2.0 and later.
See Also
-
contentSize
PropertyThe size of the content view.
Discussion
The unit of size is points. The default size is
CGSizeZero
.Availability
Available in iOS 2.0 and later.
-
contentInset
PropertyThe distance that the content view is inset from the enclosing scroll view.
Declaration
SWIFT
var contentInset: UIEdgeInsets
OBJECTIVE-C
@property(nonatomic) UIEdgeInsets contentInset
Discussion
Use this property to add to the scrolling area around the content. The unit of size is points. The default value is
UIEdgeInsetsZero
.Availability
Available in iOS 2.0 and later.
-
-
scrollEnabled
PropertyA Boolean value that determines whether scrolling is enabled.
Declaration
SWIFT
var scrollEnabled: Bool
OBJECTIVE-C
@property(nonatomic, getter=isScrollEnabled) BOOL scrollEnabled
Discussion
If the value of this property is
YES
, scrolling is enabled, and if it isNO
, scrolling is disabled. The default isYES
.When scrolling is disabled, the scroll view does not accept touch events; it forwards them up the responder chain.
Availability
Available in iOS 2.0 and later.
-
directionalLockEnabled
PropertyA Boolean value that determines whether scrolling is disabled in a particular direction.
Declaration
SWIFT
var directionalLockEnabled: Bool
OBJECTIVE-C
@property(nonatomic, getter=isDirectionalLockEnabled) BOOL directionalLockEnabled
Discussion
If this property is
NO
, scrolling is permitted in both horizontal and vertical directions. If this property isYES
and the user begins dragging in one general direction (horizontally or vertically), the scroll view disables scrolling in the other direction. If the drag direction is diagonal, then scrolling will not be locked and the user can drag in any direction until the drag completes. The default value isNO
Availability
Available in iOS 2.0 and later.
-
scrollsToTop
PropertyA Boolean value that controls whether the scroll-to-top gesture is enabled.
Discussion
The scroll-to-top gesture is a tap on the status bar. When a user makes this gesture, the system asks the scroll view closest to the status bar to scroll to the top. If that scroll view has
scrollsToTop
set toNO
, its delegate returnsNO
fromscrollViewShouldScrollToTop:
, or the content is already at the top, nothing happens.After the scroll view scrolls to the top of the content view, it sends the delegate a
scrollViewDidScrollToTop:
message.The default value of
scrollsToTop
isYES
.Special Considerations
On iPhone, the scroll-to-top gesture has no effect if there is more than one scroll view on-screen that has
scrollsToTop
set toYES
.Availability
Available in iOS 2.0 and later.
-
Scrolls a specific area of the content so that it is visible in the receiver.
Declaration
OBJECTIVE-C
- (void)scrollRectToVisible:(CGRect)
rect
animated:(BOOL)animated
Parameters
rect
A rectangle defining an area of the content view. The rectangle should be in the coordinate space of the scroll view.
animated
YES
if the scrolling should be animated,NO
if it should be immediate.Discussion
This method scrolls the content view so that the area defined by
rect
is just visible inside the scroll view. If the area is already visible, the method does nothing.Availability
Available in iOS 2.0 and later.
-
pagingEnabled
PropertyA Boolean value that determines whether paging is enabled for the scroll view.
Declaration
SWIFT
var pagingEnabled: Bool
OBJECTIVE-C
@property(nonatomic, getter=isPagingEnabled) BOOL pagingEnabled
Discussion
If the value of this property is
YES
, the scroll view stops on multiples of the scroll view’s bounds when the user scrolls. The default value isNO
.Availability
Available in iOS 2.0 and later.
-
bounces
PropertyA Boolean value that controls whether the scroll view bounces past the edge of content and back again.
Discussion
If the value of this property is
YES
, the scroll view bounces when it encounters a boundary of the content. Bouncing visually indicates that scrolling has reached an edge of the content. If the value isNO
, scrolling stops immediately at the content boundary without bouncing. The default value isYES
.Availability
Available in iOS 2.0 and later.
-
alwaysBounceVertical
PropertyA Boolean value that determines whether bouncing always occurs when vertical scrolling reaches the end of the content.
Declaration
SWIFT
var alwaysBounceVertical: Bool
OBJECTIVE-C
@property(nonatomic) BOOL alwaysBounceVertical
Discussion
If this property is set to
YES
andbounces
isYES
, vertical dragging is allowed even if the content is smaller than the bounds of the scroll view. The default value isNO
.Availability
Available in iOS 2.0 and later.
See Also
-
alwaysBounceHorizontal
PropertyA Boolean value that determines whether bouncing always occurs when horizontal scrolling reaches the end of the content view.
Declaration
SWIFT
var alwaysBounceHorizontal: Bool
OBJECTIVE-C
@property(nonatomic) BOOL alwaysBounceHorizontal
Discussion
If this property is set to
YES
andbounces
isYES
, horizontal dragging is allowed even if the content is smaller than the bounds of the scroll view. The default value isNO
.Availability
Available in iOS 2.0 and later.
See Also
-
Overridden by subclasses to customize the default behavior when a finger touches down in displayed content.
Declaration
Parameters
touches
A set of
UITouch
instances that represent the touches for the starting phase of the event represented byevent
.event
An object representing the event to which the touch objects in
touches
belong.view
The subview in the content where the touch-down gesture occurred.
Return Value
Return
NO
if you don’t want the scroll view to send event messages toview
. If you wantview
to receive those messages, returnYES
(the default).Discussion
The default behavior of
UIScrollView
is to invoke theUIResponder
event-handling methods of the target subview that the touches occur in.Availability
Available in iOS 2.0 and later.
See Also
-
Returns whether to cancel touches related to the content subview and start dragging.
Declaration
OBJECTIVE-C
- (BOOL)touchesShouldCancelInContentView:(UIView *)
view
Parameters
view
The view object in the content that is being touched.
Return Value
YES
to cancel further touch messages toview
,NO
to haveview
continue to receive those messages. The default returned value isYES
ifview
is not aUIControl
object; otherwise, it returnsNO
.Discussion
The scroll view calls this method just after it starts sending tracking messages to the content view. If it receives
NO
from this method, it stops dragging and forwards the touch events to the content subview. The scroll view does not call this method if the value of thecanCancelContentTouches
property isNO
.Availability
Available in iOS 2.0 and later.
-
canCancelContentTouches
PropertyA Boolean value that controls whether touches in the content view always lead to tracking.
Declaration
SWIFT
var canCancelContentTouches: Bool
OBJECTIVE-C
@property(nonatomic) BOOL canCancelContentTouches
Discussion
If the value of this property is
YES
and a view in the content has begun tracking a finger touching it, and if the user drags the finger enough to initiate a scroll, the view receives atouchesCancelled:withEvent:
message and the scroll view handles the touch as a scroll. If the value of this property isNO
, the scroll view does not scroll regardless of finger movement once the content view starts tracking.Availability
Available in iOS 2.0 and later.
-
delaysContentTouches
PropertyA Boolean value that determines whether the scroll view delays the handling of touch-down gestures.
Declaration
SWIFT
var delaysContentTouches: Bool
OBJECTIVE-C
@property(nonatomic) BOOL delaysContentTouches
Discussion
If the value of this property is
YES
, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent. If the value isNO
, the scroll view immediately callstouchesShouldBegin:withEvent:inContentView:
. The default value isYES
.See the class description for a fuller discussion.
Availability
Available in iOS 2.0 and later.
-
decelerationRate
PropertyA floating-point value that determines the rate of deceleration after the user lifts their finger.
Declaration
SWIFT
var decelerationRate: CGFloat
OBJECTIVE-C
@property(nonatomic) CGFloat decelerationRate
Discussion
Your application can use the
UIScrollViewDecelerationRateNormal
andUIScrollViewDecelerationRateFast
constants as reference points for reasonable deceleration rates.Availability
Available in iOS 3.0 and later.
-
dragging
PropertyA Boolean value that indicates whether the user has begun scrolling the content. (read-only)
Declaration
SWIFT
var dragging: Bool { get }
OBJECTIVE-C
@property(nonatomic, readonly, getter=isDragging) BOOL dragging
Discussion
The value held by this property might require some time or distance of scrolling before it is set to
YES
.Availability
Available in iOS 2.0 and later.
See Also
-
tracking
PropertyReturns whether the user has touched the content to initiate scrolling. (read-only)
Declaration
SWIFT
var tracking: Bool { get }
OBJECTIVE-C
@property(nonatomic, readonly, getter=isTracking) BOOL tracking
Discussion
The value of this property is
YES
if the user has touched the content view but might not have yet have started dragging it.Availability
Available in iOS 2.0 and later.
See Also
-
decelerating
PropertyReturns whether the content is moving in the scroll view after the user lifted their finger. (read-only)
Declaration
SWIFT
var decelerating: Bool { get }
OBJECTIVE-C
@property(nonatomic, readonly, getter=isDecelerating) BOOL decelerating
Discussion
The returned value is
YES
if user isn't dragging the content but scrolling is still occurring.Availability
Available in iOS 2.0 and later.
-
-
indicatorStyle
PropertyThe style of the scroll indicators.
Declaration
SWIFT
var indicatorStyle: UIScrollViewIndicatorStyle
OBJECTIVE-C
@property(nonatomic) UIScrollViewIndicatorStyle indicatorStyle
Discussion
The default style is
UIScrollViewIndicatorStyleDefault
. See Scroll Indicator Style for descriptions of these constants.Availability
Available in iOS 2.0 and later.
-
scrollIndicatorInsets
PropertyThe distance the scroll indicators are inset from the edge of the scroll view.
Declaration
SWIFT
var scrollIndicatorInsets: UIEdgeInsets
OBJECTIVE-C
@property(nonatomic) UIEdgeInsets scrollIndicatorInsets
Discussion
The default value is
UIEdgeInsetsZero
.Availability
Available in iOS 2.0 and later.
-
showsHorizontalScrollIndicator
PropertyA Boolean value that controls whether the horizontal scroll indicator is visible.
Declaration
SWIFT
var showsHorizontalScrollIndicator: Bool
OBJECTIVE-C
@property(nonatomic) BOOL showsHorizontalScrollIndicator
Discussion
The default value is
YES
. The indicator is visible while tracking is underway and fades out after tracking.Availability
Available in iOS 2.0 and later.
-
showsVerticalScrollIndicator
PropertyA Boolean value that controls whether the vertical scroll indicator is visible.
Declaration
SWIFT
var showsVerticalScrollIndicator: Bool
OBJECTIVE-C
@property(nonatomic) BOOL showsVerticalScrollIndicator
Discussion
The default value is
YES
. The indicator is visible while tracking is underway and fades out after tracking.Availability
Available in iOS 2.0 and later.
-
Displays the scroll indicators momentarily.
Declaration
SWIFT
func flashScrollIndicators()
OBJECTIVE-C
- (void)flashScrollIndicators
Discussion
You should call this method whenever you bring the scroll view to front.
Availability
Available in iOS 2.0 and later.
-
-
panGestureRecognizer
PropertyThe underlying gesture recognizer for pan gestures. (read-only)
Declaration
SWIFT
var panGestureRecognizer: UIPanGestureRecognizer { get }
OBJECTIVE-C
@property(nonatomic, readonly) UIPanGestureRecognizer *panGestureRecognizer
Discussion
Your application accesses this property when it wants to more precisely control which pan gestures are recognized by the scroll view.
Availability
Available in iOS 5.0 and later.
-
pinchGestureRecognizer
PropertyThe underlying gesture recognizer for pinch gestures. (read-only)
Declaration
SWIFT
var pinchGestureRecognizer: UIPinchGestureRecognizer? { get }
OBJECTIVE-C
@property(nonatomic, readonly) UIPinchGestureRecognizer *pinchGestureRecognizer
Discussion
Your application accesses this property when it wants to more precisely control which pinch gestures are recognized by the scroll view.
Availability
Available in iOS 5.0 and later.
-
Zooms to a specific area of the content so that it is visible in the receiver.
Declaration
OBJECTIVE-C
- (void)zoomToRect:(CGRect)
rect
animated:(BOOL)animated
Parameters
rect
A rectangle defining an area of the content view. The rectangle should be in the coordinate space of the view returned by
viewForZoomingInScrollView:
.animated
YES
if the scrolling should be animated,NO
if it should be immediate.Discussion
This method zooms so that the content view becomes the area defined by
rect
, adjusting thezoomScale
as necessary.Availability
Available in iOS 3.0 and later.
-
zoomScale
PropertyA floating-point value that specifies the current scale factor applied to the scroll view's content.
Discussion
This value determines how much the content is currently scaled. The default value is
1.0
.Availability
Available in iOS 3.0 and later.
-
A floating-point value that specifies the current zoom scale.
Declaration
SWIFT
func setZoomScale(_
scale
: CGFloat,
animatedanimated
: Bool)OBJECTIVE-C
- (void)setZoomScale:(CGFloat)
scale
animated:(BOOL)animated
Parameters
scale
The new value to scale the content to.
animated
YES
to animate the transition to the new scale,NO
to make the transition immediate.Discussion
The new scale value should be between the
minimumZoomScale
and themaximumZoomScale
.Availability
Available in iOS 3.0 and later.
-
maximumZoomScale
PropertyA floating-point value that specifies the maximum scale factor that can be applied to the scroll view's content.
Declaration
SWIFT
var maximumZoomScale: CGFloat
OBJECTIVE-C
@property(nonatomic) CGFloat maximumZoomScale
Discussion
This value determines how large the content can be scaled. It must be greater than the minimum zoom scale for zooming to be enabled. The default value is
1.0
.Availability
Available in iOS 2.0 and later.
-
minimumZoomScale
PropertyA floating-point value that specifies the minimum scale factor that can be applied to the scroll view's content.
Declaration
SWIFT
var minimumZoomScale: CGFloat
OBJECTIVE-C
@property(nonatomic) CGFloat minimumZoomScale
Discussion
This value determines how small the content can be scaled. The default value is
1.0
Availability
Available in iOS 2.0 and later.
-
zoomBouncing
PropertyA Boolean value that indicates that zooming has exceeded the scaling limits specified for the receiver. (read-only)
Declaration
SWIFT
var zoomBouncing: Bool { get }
OBJECTIVE-C
@property(nonatomic, readonly, getter=isZoomBouncing) BOOL zoomBouncing
Discussion
The value of this property is
YES
if the scroll view is zooming back to a minimum or maximum zoom scaling value; otherwise the value isNO
.Availability
Available in iOS 2.0 and later.
-
zooming
PropertyA Boolean value that indicates whether the content view is currently zooming in or out. (read-only)
Declaration
SWIFT
var zooming: Bool { get }
OBJECTIVE-C
@property(nonatomic, readonly, getter=isZooming) BOOL zooming
Discussion
The value of this property is
YES
if user is making a zoom gesture, otherwise it isNO
.Availability
Available in iOS 2.0 and later.
-
bouncesZoom
PropertyA Boolean value that determines whether the scroll view animates the content scaling when the scaling exceeds the maximum or minimum limits.
Discussion
If the value of this property is
YES
and zooming exceeds either the maximum or minimum limits for scaling, the scroll view temporarily animates the content scaling just past these limits before returning to them. If this property isNO
, zooming stops immediately at one a scaling limits. The default isYES
.Availability
Available in iOS 2.0 and later.
-
-
delegate
PropertyThe delegate of the scroll-view object.
Declaration
SWIFT
weak var delegate: UIScrollViewDelegate?
OBJECTIVE-C
@property(nonatomic, weak) id< UIScrollViewDelegate > delegate
Discussion
The delegate must adopt the
UIScrollViewDelegate
protocol. TheUIScrollView
class, which does not retain the delegate, invokes each protocol method the delegate implements.Availability
Available in iOS 2.0 and later.
-
-
keyboardDismissMode
PropertyThe manner in which the keyboard is dismissed when a drag begins in the scroll view.
Declaration
SWIFT
var keyboardDismissMode: UIScrollViewKeyboardDismissMode
OBJECTIVE-C
@property(nonatomic) UIScrollViewKeyboardDismissMode keyboardDismissMode
Discussion
See UIScrollViewKeyboardDismissMode for possible values. The default value is
UIScrollViewKeyboardDismissModeNone
.Availability
Available in iOS 7.0 and later.
-
Constants
-
The style of the scroll indicators. You use these constants to set the value of the
indicatorStyle
style.Declaration
SWIFT
enum UIScrollViewIndicatorStyle : Int { case Default case Black case White }
OBJECTIVE-C
typedef enum : NSInteger { UIScrollViewIndicatorStyleDefault, UIScrollViewIndicatorStyleBlack, UIScrollViewIndicatorStyleWhite } UIScrollViewIndicatorStyle;
Constants
-
UIScrollViewIndicatorStyleDefault
The default style of scroll indicator, which is black with a white border. This style is good against any content background.
Available in iOS 2.0 and later.
-
UIScrollViewIndicatorStyleBlack
A style of indicator which is black and smaller than the default style. This style is good against a white content background.
Available in iOS 2.0 and later.
-
UIScrollViewIndicatorStyleWhite
A style of indicator is white and smaller than the default style. This style is good against a black content background.
Available in iOS 2.0 and later.
Import Statement
OBJECTIVE-C
@import UIKit;
SWIFT
import UIKit
Availability
Available in iOS 2.0 and later.
-
-
The rate of deceleration for a scrolling view.
Declaration
SWIFT
let UIScrollViewDecelerationRateNormal: CGFloat let UIScrollViewDecelerationRateFast: CGFloat
OBJECTIVE-C
const float UIScrollViewDecelerationRateNormal; const float UIScrollViewDecelerationRateFast;
Constants
-
The manner in which the keyboard is dismissed when a drag begins in the scroll view.
Declaration
SWIFT
enum UIScrollViewKeyboardDismissMode : Int { case None case OnDrag case Interactive }
OBJECTIVE-C
typedef enum : NSInteger { UIScrollViewKeyboardDismissModeNone, UIScrollViewKeyboardDismissModeOnDrag, UIScrollViewKeyboardDismissModeInteractive }UIScrollViewKeyboardDismissMode;
Constants
-
UIScrollViewKeyboardDismissModeNone
The keyboard does not get dismissed with a drag.
Available in iOS 7.0 and later.
-
UIScrollViewKeyboardDismissModeOnDrag
The keyboard is dismissed when a drag begins.
Available in iOS 7.0 and later.
-
UIScrollViewKeyboardDismissModeInteractive
The keyboard follows the dragging touch offscreen, and can be pulled upward again to cancel the dismiss.
Available in iOS 7.0 and later.
Import Statement
OBJECTIVE-C
@import UIKit;
SWIFT
import UIKit
Availability
Available in iOS 7.0 and later.
-