Tom*_*zdz 5 iphone uiscrollview ios
我有一个UIView内部UIScrollView.每当UIScrollView变焦的变化,我想重绘整个UIView在新的缩放级别.
在iOS <3.2中,我通过调整UIView内部大小UIScrollView来使其成为新大小,然后将转换设置回Identity,以便它不会尝试进一步调整大小.但是,如果iOS> = 3.2,更改标识也会更改UIScrollView缩放比例属性.
结果是每当我缩放(比如说2x)时,我都会将嵌入器调整UIView为合适的大小,并重新绘制它.但是现在(因为我将转换重置为Identity),UIScrollView再次在zoomScale 1处思考它,而不是zoomScale 2.因此,如果我将maxZoomScale设置为2,它仍然会尝试进一步缩放,这是错误的.
我想过使用它CATiledLayer,但我不认为这对我来说已经足够了,因为我想在每次缩放后重绘,而不仅仅是像它试图做的某些缩放阈值.
有谁知道如何正确重绘UIView缩放?
Jac*_*ues 15
汤姆,
你的问题有点陈旧,但我想出了一个解决方案,所以我想我会给你一个答案,以防它或其他任何人帮助你.基本技巧是滚动视图的重置zoomScale为1,然后调整minimumZoomScale,并maximumZoomScale使得用户仍然可以放大和缩小预期.
在我的实现中,我已经UIScrollView将其子类化并将其设置为自己的委托.在我的子类中,我实现了缩放所需的两个委托方法(如下所示).contentView是我添加到UIScrollView子类的属性,为了给它实际显示内容的视图.
所以,我的init方法看起来是这样的(kMinimumZoomScale并且kMaximumZoomScale是#define的在类的顶部):
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.autoresizesSubviews = YES;
self.showsVerticalScrollIndicator = YES;
self.showsHorizontalScrollIndicator = NO;
self.bouncesZoom = YES;
self.alwaysBounceVertical = YES;
self.delegate = self;
self.minimumZoomScale = kMinimumZoomScale;
self.maximumZoomScale = kMaximumZoomScale;
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
然后我实现了UIScrollView用于缩放的标准委托方法.我的ContentView类有一个名为zoomScale的属性,告诉它使用什么比例来显示其内容.它在其drawRect方法中使用它来适当地调整内容的大小.
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)aScrollView {
return contentView;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)aScrollView withView:(UIView *)view atScale:(float)scale {
CGFloat oldZoomScale = contentView.zoomScale;
CGSize size = self.bounds.size;
// Figure out where the scroll view was centered so that we can
// fix up its offset after adjusting the scaling
CGPoint contentCenter = self.contentOffset;
contentCenter.x += size.width / (oldZoomScale * scale) / 2;
contentCenter.y += size.height / (oldZoomScale * scale) / 2;
CGFloat newZoomScale = scale * oldZoomScale;
newZoomScale = MAX(newZoomScale, kMinimumZoomscale);
newZoomScale = MIN(newZoomScale, kMaximumZoomscale);
// Set the scroll view's zoom scale back to 1 and adjust its minimum and maximum
// to allow the expected amount of zooming.
self.zoomScale = 1.0;
self.minimumZoomScale = kMinimumZoomScale / newZoomScale;
self.maximumZoomScale = kMaximumZoomScale / newZoomScale;
// Tell the contentView about its new scale. My contentView.zoomScale setter method
// calls setNeedsDisplay, but you could also call it here
contentView.zoomScale = newZoomScale;
// My ContentView class overrides sizeThatFits to give its expected size with
// zoomScale taken into account
CGRect newContentSize = [contentView sizeThatFits];
// update the content view's frame and the scroll view's contentSize with the new size
contentView.frame = CGRectMake(0, 0, newContentSize.width, newContentSize.height);
self.contentSize = newContentSize;
// Figure out the new contentOffset so that the contentView doesn't appear to move
CGPoint newContentOffset = CGPointMake(contentCenter.x - size.width / newZoomScale / 2,
contentCenter.y - size.height / newZoomScale / 2);
newContentOffset.x = MIN(newContentOffset.x, newContentSize.width - size.width);
newContentOffset.x = MAX(0, newContentOffset.x);
newContentOffset.y = MIN(newContentOffset.y, newContentSize.height - .size.height);
newContentOffset.y = MAX(0, newContentOffset.y);
[self setContentOffset:newContentOffset animated:NO];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6166 次 |
| 最近记录: |