Cocoa touch 用于漫画应用程序的UIScroll视图
我正在为一部漫画制作一个ipad应用程序,但我无法让我的滚动视图正常工作。我需要它来做:Cocoa touch 用于漫画应用程序的UIScroll视图,cocoa-touch,uiscrollview,uiimageview,zooming,autorotate,Cocoa Touch,Uiscrollview,Uiimageview,Zooming,Autorotate,我正在为一部漫画制作一个ipad应用程序,但我无法让我的滚动视图正常工作。我需要它来做: 收缩变焦 轮换 通过连环漫画双击并移动 下面是我想要得到的一个例子(第二个35+) 现在,我有捏缩放,但我可以得到滚动视图旋转和中心图像 这是我的控制器代码: #define ZOOM_VIEW_TAG 100 #define ZOOM_STEP 2 #define PAGE_TIME 10 - (void)viewDidLoad{ [super loadView]; self.my
- 收缩变焦
- 轮换
- 通过连环漫画双击并移动
#define ZOOM_VIEW_TAG 100
#define ZOOM_STEP 2
#define PAGE_TIME 10
- (void)viewDidLoad{
[super loadView];
self.myImage.image = [UIImage imageNamed:@"Tira01.jpg"];
[self.myImage setTag:ZOOM_VIEW_TAG];
UISwipeGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Tap)];
[(UITapGestureRecognizer *)recognizer setNumberOfTouchesRequired:1];
[(UITapGestureRecognizer *)drecognizer setNumberOfTapsRequired:1];
[[self myImage] addGestureRecognizer:recognizer];
[recognizer release];
self.drecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[(UITapGestureRecognizer *)drecognizer setNumberOfTouchesRequired:1];
[(UITapGestureRecognizer *)drecognizer setNumberOfTapsRequired:2];
[[self myImage] addGestureRecognizer:drecognizer];
[drecognizer release];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [myScrollView viewWithTag:ZOOM_VIEW_TAG];
}
- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
UIInterfaceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
if(!self.zoomed)
{
// double tap zooms in
float newScale = [self.myScrollView zoomScale] * ZOOM_STEP;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[self.myScrollView zoomToRect:zoomRect animated:YES];
self.zoomed = YES;
}
else {
float newScale = [self.myScrollView zoomScale] / ZOOM_STEP;
CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
[self.myScrollView zoomToRect:zoomRect animated:YES];
self.zoomed = NO;
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return YES;
}
- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
CGRect zoomRect;
// the zoom rect is in the content view's coordinates.
// At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
// As the zoom scale decreases, so more content is visible, the size of the rect grows.
zoomRect.size.height = [self.myScrollView frame].size.height / scale;
zoomRect.size.width = [self.myScrollView frame].size.width / scale;
// choose an origin so as to get the right center.
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
return zoomRect;
}
在interface builder中,我有:
->UIView(缩放填充模式)
-->UIScrollView(缩放填充模式)
--->UIImageView(AspectFit模式)
我不知道我做错了什么,但我对此很疯狂:(关于以内容为中心的UIScrollView,我可能在这里错了,但UIScrollView不会自动做到这一点 在我的应用程序中,我通过实际移动scrollView中的内容来手动操作,使其居中。
正如下面代码的快速解释一样,如果内容(图像)帧小于滚动视图的边界,则水平和垂直调整内容的位置 下面的代码来自我从UIScrollView派生的类。如果您使用默认的UIScrollView,则可以在父UIView中使用此代码
- (void)layoutSubviews
{
[super layoutSubviews];
if (![self centersContent])
{
return;
}
// center the image as it becomes smaller than the size of the screen
CGSize boundsSize = self.bounds.size;
CGRect frameToCenter = imageView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width)
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
frameToCenter.origin.x = 0;
// center vertically
if (frameToCenter.size.height < boundsSize.height)
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
frameToCenter.origin.y = 0;
imageView.frame = frameToCenter;
}
-(无效)布局子视图
{
[超级布局子视图];
如果(![自我中心内容])
{
返回;
}
//当图像小于屏幕尺寸时,将图像居中
CGSize boundsSize=self.bounds.size;
CGRect frameToCenter=imageView.frame;
//水平居中
if(frameToCenter.size.width