我创建了一个scrollview并在其中添加了一些图像。
但现在,我无法将图片拖到scrollview之外?
我的错误是什么?
我已经提到了一些教程。
但我不知道如何继续。
我的代码:
scroll =[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height * 0.9)];
scroll.pagingEnabled=YES;
scroll.scrollEnabled = TRUE;
[scroll setBackgroundColor:[UIColor redColor]];
scroll.bounces=TRUE;
scroll.userInteractionEnabled=YES;
scroll.showsHorizontalScrollIndicator=TRUE;
[scroll setContentSize:CGSizeMake(3500, sh)];
[self.view addSubview:scroll];
for (int i=1; i <= 6; i++) {
NSString *imageName = [NSString stringWithFormat:@"img%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.origin.x = currentX;
rect.size.height=[UIScreen mainScreen].bounds.size.height * 0.9;
rect.size.width=width/4;
imageView.frame = rect;
currentX += imageView.frame.size.width;
currentX += 30;
[scroll addSubview:imageView];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] == 1) {
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
for (UIImageView *iView in self.view.subviews) {
if ([iView isMemberOfClass:[UIImageView class]]) {
if (touchPoint.x > iView.frame.origin.x &&
touchPoint.x < iView.frame.origin.x + iView.frame.size.width &&
touchPoint.y > iView.frame.origin.y &&
touchPoint.y < iView.frame.origin.y + iView.frame.size.height)
{
self.sizePosition=CGPointMake(iView.frame.size.width, iView.frame.size.height);
self.dragObject = iView;
self.touchOffset = CGPointMake(touchPoint.x - iView.frame.origin.x,
touchPoint.y - iView.frame.origin.y);
self.homePosition = CGPointMake(iView.frame.origin.x,
iView.frame.origin.y);
[self.view bringSubviewToFront:self.dragObject];
self.sizebig=CGPointMake(iView.frame.size.width+20,iView.frame.size.height+25);
}
}
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
CGRect newDragObjectFrame = CGRectMake(touchPoint.x - touchOffset.x,
touchPoint.y - touchOffset.y,
self.sizebig.x,
self.sizebig.y);
self.dragObject.frame = newDragObjectFrame;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
if (touchPoint.x > self.dropTarget.frame.origin.x &&
touchPoint.x < self.dropTarget.frame.origin.x + self.dropTarget.frame.size.width &&
touchPoint.y > self.dropTarget.frame.origin.y &&
touchPoint.y < self.dropTarget.frame.origin.y + self.dropTarget.frame.size.height )
{
self.droptargett.image=self.dragObject.image;
}
self.dragObject.frame = CGRectMake(self.homePosition.x,self.homePosition.y,sizePosition.x,sizePosition.y);
}