سؤال

أرغب في تطبيق UisCrollView حيث يتم تمكين الترحيل ويمكنني فقط النقر على بعض الصور. هذا كل ما أريد أن أكون قادرًا على فعله الآن.

لقد فعلت هذا حتى الآن في Builder Interface: هل يمكن لأحد أن يساعد؟

alt text

أنا لا أعرف كيف أفعل الباقي. هل يمكن لشخص ما مساعدتي في هذا. لا أحتاج إلى أي وظيفة التكبير. لا أريد أي معاينة للصورة السابقة أو التالية داخل ScrollView ، أريد فقط عرض تمرير بسيط للتمكين يتيح للمستخدم النفض عبر الصور.

يتم تقدير كل المساعدة. إذا تمكنت من أن تخبرني خطوة بخطوة كيف يمكنني تحقيق ذلك ، فستكون موضع تقدير كبير. شكرا لك.

لقد نظرت إلى أمثلة الكود ولديهم الكثير من التعقيد. نظرت إلى عدة وأفضل برنامج تعليمي من البداية. شكرا لك

هل كانت مفيدة؟

المحلول

يبدو أنك تحتاج فقط إلى إضافة المحتوى الخاص بك كإظهر فرعي لـ UIScrollView وإضافة معرف الإيماءات.

قم بتحميل صورتك في uiimageView. أضف uiimageView كمظهر فرعي لـ UisCrollView.

// do this in init or loadView or viewDidLoad, wherever is most appropriate
// imageView is a retained property
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.png"];
[scrollView addSubview:imageView];

أضف uiswipegesturerecognizer إلى UisCrollView.

// probably after the code above
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:);
[scrollView addGestureRecognizer:swipe];
[swipe release];

على معالج UISwipeGesturerEcognizer ، قم بتغيير الصورة المحملة في UiimageView.

- (void)handleSwipe:(UIGestureRecognizer *)swipe {
  // do what you need to determine the next image
  imageView.image = [UIImage imageNamed:<your replacement image here>];
}

نصائح أخرى

ربما تريد إلقاء نظرة على بلدي تنفيذ العينة من ViewController الذي يفعل هذا بالضبط. لقد كتبت هذا الشيء كإجابة على هذا السؤال.
ربما يكون هذا معقدًا جدًا بالنسبة لك ، لكنه لن يصبح أسهل.
وهذا هو الإصدار الأساسي فقط ، والذي يقوم بتحميل جميع الصور في الذاكرة في البداية. هذا لن يعمل في تطبيق حقيقي. لذلك عليك تنفيذ بعض وظائف UIScrollView-Delegate. وهناك يبدأ التعقيد ...

//  ImageViewController.h
//
//  Created by Matthias Bauch on 12.10.10.
//  Copyright 2010 Matthias Bauch. All rights reserved.
//

#import <UIKit/UIKit.h>

#warning this is just a quick hack, you should not use this if you dont understand this. There might be leaks, bugs and a lot of whatever.

@interface ImageViewController : UIViewController {
    NSString *imagePath;
}
@property (nonatomic, copy) NSString *imagePath;
- (id)initWithImageDirectory:(NSString*)imgPath;
@end


//
//  ImageViewController.m
//
//  Created by Matthias Bauch on 12.10.10.
//  Copyright 2010 Matthias Bauch. All rights reserved.
//

#import "ImageViewController.h"


@implementation ImageViewController
@synthesize imagePath;

- (id)initWithImageDirectory:(NSString*)imgPath {
    if (self = [super init]) {
        imagePath = [imgPath copy];
    }
    return self;
}


- (UIView *)viewFullOfImagesAtPath:(NSString *)path withSize:(CGSize)size {
    NSError *error = nil;
    NSArray *filenames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
    if (!filenames) {
        NSLog(@"Error accessing files: %@ [%@]", [error localizedDescription], error);
        return nil;
    }
    UIView *aView = [[UIView alloc] init];
    CGFloat xOffset = 0;
    for (NSString *filename in filenames) {
        NSString *fullPath = [path stringByAppendingPathComponent:filename];
        UIImage *image = [[[UIImage alloc] initWithContentsOfFile:fullPath] autorelease];
        if (!image)
            continue;
        CGRect frameRect = CGRectMake(xOffset, 0, size.width, size.height);
        UIImageView *imageView = [[[UIImageView alloc] initWithFrame:frameRect] autorelease];
        [imageView setImage:image];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [aView addSubview:imageView];
        xOffset += size.width;
    }
    aView.frame = CGRectMake(0, 0, xOffset, size.height);
    return [aView autorelease];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    UIScrollView *scrollView = [[[UIScrollView alloc] initWithFrame:self.view.bounds] autorelease];
    scrollView.pagingEnabled = YES;
    UIView *contentView = [self viewFullOfImagesAtPath:imagePath withSize:CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height)];
    NSLog(@"%f %f %f %f", contentView.frame.origin.x, contentView.frame.origin.y, contentView.frame.size.width, contentView.frame.size.height);
    [scrollView addSubview:contentView];
    scrollView.contentSize = CGSizeMake(CGRectGetWidth(contentView.frame), CGRectGetHeight(contentView.frame));
    [self.view addSubview:scrollView];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


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


@end
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top