iPhone - 我的新视图挡住了我的工具栏

发布于 2024-10-15 13:21:27 字数 6005 浏览 3 评论 0原文

我的应用程序有问题。在教程的帮助下,我创建了带有图片的幻灯片。我添加了一个工具栏,以便可以返回。问题是当图片显示时,工具栏消失了。我一直在尝试google这个问题等等,但无法解决。在“上一个”类中,我创建了 SlideShowViewController,并在该类中创建了另一个名为 SlideShowView 的类。我希望这不会太混乱或愚蠢,因为我真的需要帮助。

这是代码:

#import "SlideShowViewController.h"
#import "Pictures.h"
@interface SlideShowView : UIView
{
    NSArray * mImages;
    UIImageView * mLeftImageView;
    UIImageView * mCurrentImageView;
    UIImageView * mRightImageView;
    NSUInteger mCurrentImage;
    Pictures *picRef;
    SlideShowViewController *slideRef;
    BOOL mSwiping;
    CGFloat mSwipeStart;
}
- (id)initWithImages:(NSArray *)inImages;
@end // SlideShowView


#pragma mark -
//#import "SlideShowViewController.h"
@implementation SlideShowView

- (UIImageView *)createImageView:(NSUInteger)inImageIndex
{
    if (inImageIndex >= [mImages count])
    {
        return nil;
    }

    UIImageView * result = [[UIImageView alloc] initWithImage:[mImages objectAtIndex:inImageIndex]];
    result.opaque = YES;
    result.userInteractionEnabled = NO;
    result.backgroundColor = [UIColor whiteColor];
    result.contentMode = UIViewContentModeScaleAspectFit;
    result.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    return result;
}

- (id)initWithImages:(NSArray *)inImages
{
    if (self = [super initWithFrame:CGRectZero])
    {
        mImages = [inImages retain];

        NSUInteger imageCount = [inImages count];
        if (imageCount > 0)
        {
            mCurrentImageView = [self createImageView:0];

            [self addSubview:mCurrentImageView];

            if (imageCount > 1)
            {
                mRightImageView = [self createImageView:1];
                [self addSubview:mRightImageView];
            }

        }

        self.opaque = YES;
        self.backgroundColor = [UIColor whiteColor];
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    }

    return self;
}

- (void)dealloc
{
    [mImages release];
    [super dealloc];
}



- (void)layoutSubviews
{
    if (mSwiping)
        return;

    CGSize contentSize = self.frame.size;
    mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
    mCurrentImageView.frame = CGRectMake(0.0f, 0.0f, contentSize.width, contentSize.height);
    mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);


}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([touches count] != 1){
        return;
    }

    mSwipeStart = [[touches anyObject] locationInView:self].x;
    mSwiping = YES;

    mLeftImageView.hidden = NO;
    mCurrentImageView.hidden = NO;
    mRightImageView.hidden = NO;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (! mSwiping || [touches count] != 1)
        return;

    CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;

    CGSize contentSize = self.frame.size;

    mLeftImageView.frame = CGRectMake(swipeDistance - contentSize.width, 0.0f, contentSize.width, contentSize.height);
    mCurrentImageView.frame = CGRectMake(swipeDistance, 0.0f, contentSize.width, contentSize.height);
    mRightImageView.frame = CGRectMake(swipeDistance + contentSize.width, 0.0f, contentSize.width, contentSize.height);
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (! mSwiping)
        return;

    CGSize contentSize = self.frame.size;

    NSUInteger count = [mImages count];

    CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;
    if (mCurrentImage > 0 && swipeDistance > 50.0f)
    {
        [mRightImageView removeFromSuperview];
        [mRightImageView release];

        mRightImageView = mCurrentImageView;
        mCurrentImageView = mLeftImageView;

        mCurrentImage--;
        if (mCurrentImage > 0)
        {
            mLeftImageView = [self createImageView:mCurrentImage - 1];
            mLeftImageView.hidden = YES;

            [self addSubview:mLeftImageView];
        }
        else
        {
            mLeftImageView = nil;
        }
    }
    else if (mCurrentImage < count - 1 && swipeDistance < -50.0f)
    {
        [mLeftImageView removeFromSuperview];
        [mLeftImageView release];

        mLeftImageView = mCurrentImageView;
        mCurrentImageView = mRightImageView;

        mCurrentImage++;
        if (mCurrentImage < count - 1)
        {
            mRightImageView = [self createImageView:mCurrentImage + 1];
            mRightImageView.hidden = YES;

            [self addSubview:mRightImageView];
        }
        else
        {
            mRightImageView = nil;
        }
    }

    [UIView beginAnimations:@"swipe" context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:0.3f];

    mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
    mCurrentImageView.frame = CGRectMake(0.0f, 0.0f, contentSize.width, contentSize.height);
    mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);

    [UIView commitAnimations];
    mSwiping = NO;
}



@end // SlideShowView


#pragma mark -


@implementation SlideShowViewController
@synthesize toolbar;

-(IBAction) goBack:(id) sender{
    [self.parentViewController dismissModalViewControllerAnimated:YES];
    }

- (id)init
{

    if (self = [super initWithNibName:nil bundle:nil])
    {
        NSArray * images = [NSArray arrayWithObjects:[UIImage imageNamed:@"bifColor.png"],
                            [UIImage imageNamed:@"DSCF1600.jpg"],
                            [UIImage imageNamed:@"refresh.png"],
                            [UIImage imageNamed:@"DSCF1601.jpg"], nil];
        self.view = [[[SlideShowView alloc] initWithImages:images] autorelease];
    }

    return self;
}

 @end

I have a problem with my app. With help by a tutorial I've created a slideshow with pictures. I've added a toolbar so it's possible to go back. The problem is that when the pictures are showing, the toolbar disappear. I've been trying to google the problem and so on, but I can't solve it. In a "previous" class I create SlideShowViewController, and inside that class I have another one called SlideShowView. I hope this ain't to messy or stupid, cause I really need help.

Here is the code:

#import "SlideShowViewController.h"
#import "Pictures.h"
@interface SlideShowView : UIView
{
    NSArray * mImages;
    UIImageView * mLeftImageView;
    UIImageView * mCurrentImageView;
    UIImageView * mRightImageView;
    NSUInteger mCurrentImage;
    Pictures *picRef;
    SlideShowViewController *slideRef;
    BOOL mSwiping;
    CGFloat mSwipeStart;
}
- (id)initWithImages:(NSArray *)inImages;
@end // SlideShowView


#pragma mark -
//#import "SlideShowViewController.h"
@implementation SlideShowView

- (UIImageView *)createImageView:(NSUInteger)inImageIndex
{
    if (inImageIndex >= [mImages count])
    {
        return nil;
    }

    UIImageView * result = [[UIImageView alloc] initWithImage:[mImages objectAtIndex:inImageIndex]];
    result.opaque = YES;
    result.userInteractionEnabled = NO;
    result.backgroundColor = [UIColor whiteColor];
    result.contentMode = UIViewContentModeScaleAspectFit;
    result.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    return result;
}

- (id)initWithImages:(NSArray *)inImages
{
    if (self = [super initWithFrame:CGRectZero])
    {
        mImages = [inImages retain];

        NSUInteger imageCount = [inImages count];
        if (imageCount > 0)
        {
            mCurrentImageView = [self createImageView:0];

            [self addSubview:mCurrentImageView];

            if (imageCount > 1)
            {
                mRightImageView = [self createImageView:1];
                [self addSubview:mRightImageView];
            }

        }

        self.opaque = YES;
        self.backgroundColor = [UIColor whiteColor];
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    }

    return self;
}

- (void)dealloc
{
    [mImages release];
    [super dealloc];
}



- (void)layoutSubviews
{
    if (mSwiping)
        return;

    CGSize contentSize = self.frame.size;
    mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
    mCurrentImageView.frame = CGRectMake(0.0f, 0.0f, contentSize.width, contentSize.height);
    mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);


}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([touches count] != 1){
        return;
    }

    mSwipeStart = [[touches anyObject] locationInView:self].x;
    mSwiping = YES;

    mLeftImageView.hidden = NO;
    mCurrentImageView.hidden = NO;
    mRightImageView.hidden = NO;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (! mSwiping || [touches count] != 1)
        return;

    CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;

    CGSize contentSize = self.frame.size;

    mLeftImageView.frame = CGRectMake(swipeDistance - contentSize.width, 0.0f, contentSize.width, contentSize.height);
    mCurrentImageView.frame = CGRectMake(swipeDistance, 0.0f, contentSize.width, contentSize.height);
    mRightImageView.frame = CGRectMake(swipeDistance + contentSize.width, 0.0f, contentSize.width, contentSize.height);
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (! mSwiping)
        return;

    CGSize contentSize = self.frame.size;

    NSUInteger count = [mImages count];

    CGFloat swipeDistance = [[touches anyObject] locationInView:self].x - mSwipeStart;
    if (mCurrentImage > 0 && swipeDistance > 50.0f)
    {
        [mRightImageView removeFromSuperview];
        [mRightImageView release];

        mRightImageView = mCurrentImageView;
        mCurrentImageView = mLeftImageView;

        mCurrentImage--;
        if (mCurrentImage > 0)
        {
            mLeftImageView = [self createImageView:mCurrentImage - 1];
            mLeftImageView.hidden = YES;

            [self addSubview:mLeftImageView];
        }
        else
        {
            mLeftImageView = nil;
        }
    }
    else if (mCurrentImage < count - 1 && swipeDistance < -50.0f)
    {
        [mLeftImageView removeFromSuperview];
        [mLeftImageView release];

        mLeftImageView = mCurrentImageView;
        mCurrentImageView = mRightImageView;

        mCurrentImage++;
        if (mCurrentImage < count - 1)
        {
            mRightImageView = [self createImageView:mCurrentImage + 1];
            mRightImageView.hidden = YES;

            [self addSubview:mRightImageView];
        }
        else
        {
            mRightImageView = nil;
        }
    }

    [UIView beginAnimations:@"swipe" context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:0.3f];

    mLeftImageView.frame = CGRectMake(-contentSize.width, 0.0f, contentSize.width, contentSize.height);
    mCurrentImageView.frame = CGRectMake(0.0f, 0.0f, contentSize.width, contentSize.height);
    mRightImageView.frame = CGRectMake(contentSize.width, 0.0f, contentSize.width, contentSize.height);

    [UIView commitAnimations];
    mSwiping = NO;
}



@end // SlideShowView


#pragma mark -


@implementation SlideShowViewController
@synthesize toolbar;

-(IBAction) goBack:(id) sender{
    [self.parentViewController dismissModalViewControllerAnimated:YES];
    }

- (id)init
{

    if (self = [super initWithNibName:nil bundle:nil])
    {
        NSArray * images = [NSArray arrayWithObjects:[UIImage imageNamed:@"bifColor.png"],
                            [UIImage imageNamed:@"DSCF1600.jpg"],
                            [UIImage imageNamed:@"refresh.png"],
                            [UIImage imageNamed:@"DSCF1601.jpg"], nil];
        self.view = [[[SlideShowView alloc] initWithImages:images] autorelease];
    }

    return self;
}

 @end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遮了一弯 2024-10-22 13:21:27

而不是这样:

self.view = [[[SlideShowView alloc] initWithImages:images] autorelease];

这样做:

UIView* slideShowView = [[[SlideShowView alloc] initWithImages:images] autorelease];
[self.view addSubview:slideShowView];

编辑:也不要忘记设置slideShowView的框架!

Instead of this:

self.view = [[[SlideShowView alloc] initWithImages:images] autorelease];

do that:

UIView* slideShowView = [[[SlideShowView alloc] initWithImages:images] autorelease];
[self.view addSubview:slideShowView];

EDIT: Also don't forget to set the frame of slideShowView!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文