我有一个滚动视图,其中包含我绘制一些图像的UIView子类。我希望能够旋转滚动视图的内容,但我似乎无法弄清楚如何应用CGAffineTransformMakeRotation并保持滚动视图的功能(内部视图可拖动和可移动)。
我的最终目标是制作一张带有动态定位标记的地图,这些标记在使用集成罗盘旋转时可伸缩和缩放。
(你可能已经猜到我是iOS开发新手,所以我很失落)。
我的ViewController包含ScrollView:
#import "ViewController.h"
@implementation ViewController
@synthesize scrollView;
@synthesize mapView;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect mapFrame;
mapFrame.origin = CGPointMake(0, 0);
mapFrame.size = CGSizeMake(450, 476);
mapView.frame = mapFrame;
scrollView.delegate = self;
scrollView.bounces = NO;
scrollView.scrollsToTop = NO;
scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
scrollView.contentSize = CGSizeMake(450, 476);
scrollView.minimumZoomScale = 0.7;
scrollView.maximumZoomScale = 6.0;
scrollView.zoomScale = 0.7;
}
- (void)viewDidUnload
{
[self setScrollView:nil];
[self setMapView:nil];
[super viewDidUnload];
}
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return mapView;
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
}
@end
我的UIView子类嵌入在ScrollView中:
#import "MapView.h"
@implementation MapView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
// do some drawing
UIImage *myImage = [UIImage imageNamed:@"map.jpg"];
CGRect rectangle = CGRectMake(0,0,450,476);
[myImage drawInRect:rectangle];
UIImage *testAnimationImage = [UIImage imageNamed:@"star_marker.png"];
UIImageView *testAnimationView = [[UIImageView alloc] initWithImage:testAnimationImage];
testAnimationView.frame = CGRectMake(30, 30, 34, 34);
[self addSubview:testAnimationView];
// rotate the map - this is where I get stuck
CGAffineTransform transform = CGAffineTransformMakeRotation(45 / 180.0 * M_PI);
self.transform = transform;
// center the rotation point
CGRect frame = [self frame];
frame.origin.x = 450/2;
frame.origin.y = 476/2;
[self setFrame:frame];
}
@end
编辑:
将视图设置移出drawRect后,继续更新代码。
的ViewController:
#import "ViewController.h"
@implementation ViewController
@synthesize scrollView;
@synthesize mapView;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
mapView = [[MapView alloc] initWithFrame:CGRectMake(0, 0, 450, 476)];
[scrollView addSubview:mapView];
scrollView.delegate = self;
scrollView.bounces = NO;
scrollView.scrollsToTop = NO;
scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
scrollView.contentSize = CGSizeMake(450, 476);
scrollView.minimumZoomScale = 0.7;
scrollView.maximumZoomScale = 6.0;
scrollView.zoomScale = 0.7;
}
-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return mapView;
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
}
@end
MapView的:
#import "MapView.h"
@implementation MapView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self layoutView];
}
return self;
}
- (void)layoutView {
UIImage *myImage = [UIImage imageNamed:@"map.jpg"];
CGRect rectangle = CGRectMake(0,0,450,476);
UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage];
myImageView.frame = rectangle;
[self addSubview:myImageView];
UIImage *testAnimationImage = [UIImage imageNamed:@"star_marker.png"];
UIImageView *testAnimationView = [[UIImageView alloc] initWithImage:testAnimationImage];
testAnimationView.frame = CGRectMake(30, 30, 34, 34);
[self addSubview:testAnimationView];
}
- (void)drawRect:(CGRect)rect
{
}
@end