ビューで写真を左右にスワイプするコードがいくつかあります。
#import "GalleryView.h"
#import "HotelDescription.h"
#import "GalleryImageView.h"
#define IMAGE_WIDTH 550
#define BORDER_WIDTH 15
#define IMAGE_WIDTH_IPHONE 320
#define BORDER_WIDTH_IPHONE 5
BOOL isRotating = NO;
@interface GalleryView (Private)
- (NSInteger)getCurrentPosition;
- (void)setNewPosition:(NSInteger)posIndex animated:(BOOL)animated;
@end
@implementation GalleryView
@synthesize scrollDelegate;
- (void)awakeFromNib
{
[super awakeFromNib];
self.delegate = self;
self.pagingEnabled = NO;
_galleryImageViews = [[NSMutableArray alloc] init];
photoNum = 0;
}
- (void)setHotelDescription:(HotelDescription *)description
{
[_hotelDescription release];
_hotelDescription = [description retain];
[_galleryImageViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[_galleryImageViews removeAllObjects];
for (int i = 0; i < [_hotelDescription.imageURLs count]; i++)
{
GalleryImageView *giv;
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
giv = [[GalleryImageView alloc] initWithFrame:CGRectMake(0, 0, IMAGE_WIDTH, IMAGE_WIDTH/1.5)];
else
giv = [[GalleryImageView alloc] initWithFrame:CGRectMake(0, 0, IMAGE_WIDTH_IPHONE, IMAGE_WIDTH_IPHONE/1.5)];
[_galleryImageViews addObject:giv];
[self addSubview:giv];
[giv release];
}
[self setNewPosition:0 animated:NO];
}
- (void)updateImages
{
for (GalleryImageView *giv in _galleryImageViews)
{
if (giv.image == nil)
{
NSInteger index = [_galleryImageViews indexOfObject:giv];
NSString *imageURL = [_hotelDescription.imageURLs objectAtIndex:index];
NSString *filePath = [_hotelDescription filePathForImageURL:imageURL];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
[giv setImage:[UIImage imageWithContentsOfFile:filePath]];
}
}
}
}
// расположение картинок, задается их центр
- (void)updatePhotoPositions
{
for (int i = 0; i < [_galleryImageViews count]; i++)
{
CGFloat currentPhotoCenterOffset;
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
currentPhotoCenterOffset = (BORDER_WIDTH * 2 + IMAGE_WIDTH) * i;
else
currentPhotoCenterOffset = (BORDER_WIDTH_IPHONE * 2 + IMAGE_WIDTH_IPHONE) * i;
GalleryImageView *giv = [_galleryImageViews objectAtIndex:i];
giv.center = CGPointMake(self.frame.size.width/2 + currentPhotoCenterOffset, self.frame.size.height / 2);
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
CGFloat contentOffsetX = self.frame.size.width/2 - (IMAGE_WIDTH/2 + BORDER_WIDTH);
[self setContentSize:CGSizeMake(contentOffsetX * 2 + [_galleryImageViews count] * (IMAGE_WIDTH + BORDER_WIDTH * 2), self.frame.size.height)];
[self updatePhotoPositions];
}
else
{
CGFloat contentOffsetX = self.frame.size.width/2 - (IMAGE_WIDTH_IPHONE/2 + BORDER_WIDTH_IPHONE);
[self setContentSize:CGSizeMake(contentOffsetX * 2 + [_galleryImageViews count] * (IMAGE_WIDTH_IPHONE + BORDER_WIDTH_IPHONE * 2), self.frame.size.height)];
[self updatePhotoPositions];
}
if (isRotating)
[self setNewPosition:_currentPosition animated:NO];
}
- (void)willRotate
{
isRotating = YES;
}
- (void)didRotate
{
isRotating = NO;
[self setNewPosition:_currentPosition animated:NO];
}
#pragma mark - UIScrollView delegate
- (NSInteger)getCurrentPosition:(UIScrollView *)scrollView
{
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
ScrollDirection scrollDirection;
NSLog(@"last = %d and new = %f", self.lastContentOffset, scrollView.contentOffset.x);
if (self.lastContentOffset > scrollView.contentOffset.x)
{
scrollDirection = ScrollDirectionRight;
if (photoNum != 0) {
photoNum = photoNum - 1;
}
NSLog(@"Right = %d", photoNum);
}
else if (self.lastContentOffset < scrollView.contentOffset.x)
{
scrollDirection = ScrollDirectionLeft;
if (photoNum != [_galleryImageViews count] -1) {
photoNum = photoNum + 1;
}
NSLog(@"Left = %d", photoNum);
}
//self.lastContentOffset = scrollView.contentOffset.x;
/*
CGFloat imagesOffsetX = self.frame.size.width / 2 - (IMAGE_WIDTH /2 + BORDER_WIDTH);
NSLog(@"1 = %f", imagesOffsetX);
CGFloat photoOffset = self.contentOffset.x + self.frame.size.width / 2;
NSLog(@"(%f + %f)", self.contentOffset.x, self.frame.size.width);
NSLog(@"2 = %f", photoOffset);
NSLog(@"(%f - %f)/(%d + %d * 2)", photoOffset, imagesOffsetX, IMAGE_WIDTH, BORDER_WIDTH);
NSInteger index = (photoOffset - imagesOffsetX) / (IMAGE_WIDTH + BORDER_WIDTH * 2);
NSLog(@"3 = %d", index);
index = MAX(index, 0);
index = MIN(index, [_galleryImageViews count]-1);
return index;
*/
NSLog(@"phNum = %d", photoNum);
return photoNum;
}
else
{
CGFloat imagesOffsetX = self.frame.size.width / 2 - (IMAGE_WIDTH_IPHONE /2 + BORDER_WIDTH_IPHONE);
CGFloat photoOffset = self.contentOffset.x + self.frame.size.width / 2;
NSInteger index = (photoOffset - imagesOffsetX) / (IMAGE_WIDTH_IPHONE + BORDER_WIDTH_IPHONE * 2);
index = MAX(index, 0);
index = MIN(index, [_galleryImageViews count]-1);
return index;
}
}
- (void)setNewPosition:(NSInteger)posIndex animated:(BOOL)animated
{
_currentPosition = posIndex;
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
{
CGFloat currentPhotoCenterOffset = (BORDER_WIDTH * 2 + IMAGE_WIDTH) * _currentPosition;
[self setContentOffset:CGPointMake(currentPhotoCenterOffset, self.contentOffset.y) animated:animated];
}
else
{
CGFloat currentPhotoCenterOffset = (BORDER_WIDTH_IPHONE * 2 + IMAGE_WIDTH_IPHONE) * _currentPosition;
[self setContentOffset:CGPointMake(currentPhotoCenterOffset, self.contentOffset.y) animated:animated];
}
if (scrollDelegate)
[scrollDelegate galleryView:self didSelectPhotoNo:posIndex];
}
//начинает обрабатывать движения
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
[scrollView setContentOffset:scrollView.contentOffset animated:NO];
self.lastContentOffset = scrollView.contentOffset.x;
[self setNewPosition:[self getCurrentPosition:scrollView] animated:YES];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self setNewPosition:[self getCurrentPosition:scrollView] animated:YES];
}
#pragma mark - Memory management
- (void)dealloc
{
[_galleryImageViews release];
[_hotelDescription release];
[super dealloc];
}
@end
しかし、私にはいくつかのバグがあります(左にスワイプします-大丈夫ですが、右にスワイプしようとすると、写真はまだ左にジャンプします(リストの最後に到達するまで続きます。反対方向には同じバグ(
どこが間違っていたのですか?