1

そのため、フォトギャラリーのように動作するアプリがあり、ユーザーが画像を削除する機能を実装しています。設定は次のとおりです。9つのUIImageView、imageView、imageView2などがあります。[編集]ボタンとtapGestureアクションメソッドもあります。Tap Gesture RecognizerをIBのビューにドラッグし、UIImageViewのそれぞれにアタッチしました。また、各UIImageViewにtapGestureアクションメソッドをアタッチしました。理想的には、「編集」ボタンが押されたときにのみメソッドがアクティブになるようにしたいです。ユーザーが[編集]をタップしてから、削除する画像をタップすると、UIAlertViewが表示され、削除してもよいかどうかを尋ねられます。これが私が使用しているコードです:

- (IBAction)editButtonPressed:(id)sender {
    editButton.hidden = YES;
    backToGalleryButton.hidden = NO;
    tapToDeleteLabel.hidden = NO;
}

- (IBAction)tapGesture:(UITapGestureRecognizer*)gesture
{

    UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete"
                                                              message:@"Are you sure you want to delete this photo?"
                                                             delegate:self
                                                    cancelButtonTitle:@"No"
                                                    otherButtonTitles:@"Yes", nil];
    [deleteAlertView show];

    if (buttonIndex != [alertView cancelButtonIndex]) {

        UIImageView *view = [self.UIImageView];
        if (view) {
            [self.array removeObject:view];
        }


        CGPoint tapLocation = [gesture locationInView: self.view];
        for (UIImageView *imageView in self.view.subviews) {
            if (CGRectContainsPoint(self.UIImageView.frame, tapLocation)) {
                ((UIImageView *)[self.view]).image =nil;
 }

}
        [self.user setObject:self.array forKey:@"images"];
}
}

このコードは明らかにエラーでいっぱいです:この行の「宣言されていない識別子ボタンインデックスの使用」:if (buttonIndex != [alertView cancelButtonIndex])

この行の「タイプPhotoViewControllerのオブジェクトにプロパティUIImageViewが見つかりません」UIImageView *view = [self.UIImageView];

そして、この行の「期待される識別子」((UIImageView *)[self.view]).image =nil;

私はプログラミングに非常に慣れていないので、ここまでやってきたことに驚いています。したがって、エラーがなくなり、9つの画像ビューのいずれかがタップされたときにいつでも使用できるようにコードを編集する必要がある方法を理解しようとしています。また、このメソッドは、編集ボタンが最初に押されます。以前はタグを使用していましたが、うまく機能しましたが、NSDataを介して画像を保存しているため、タグを使用できなくなりました。どんな助けでも大歓迎です、ありがとう!

4

3 に答える 3

2

まず、画像ビューにタップジェスチャを添付したくありません。また、9つを超える画像がある場合は、スクロールビューが必要になるか、スクロールを個別に処理する必要があります。まず、そのジェスチャレコグナイザとそのすべての接続を削除します。

次に、ギャラリーキャンバスとして使用するビューのタイプを決定します。単純なビュー、またはScrollView、または何でも...それを機能させるためだけに、今は実際には重要ではありません。そのビューをインターフェイス定義にCtrlキーを押しながらドラッグすると、ビューのIBOutletがドロップされます(コードで参照できるようになります)。

ImageViewsを先ほど説明したビューに配置します。

ジェスチャレコグナイザの内部フラグを設定できますが、必要なときにいつでも有効/無効にできるプロパティもあります。したがって、かなり簡単にアクティブ/非アクティブにすることができます。

必要なのは、単一のタップジェスチャ認識機能をコントローラにドロップし、それをコントローラの実装セクションに接続することだけです。レコグナイザーを処理するためのスタブを生成します。コントローラの「一般的に」タップを解釈し、ビューでタップが行われるたびにコードを呼び出します。

もう少しコード...

スクロールビューで「新しい」画像のフレームを作成します。

- (CGRect)frameForData:(MyData*)data atIndex:(NSUInteger)idx
{
    CGPoint topLeft;
    int row = idx / 4;
    int col = idx % 4;
    topLeft.x = (col+1)*HORIZ_SPACING + THUMBNAIL_WIDTH * (col);
    topLeft.y = (row+1)*VERT_SPACING + THUMBNAIL_HEIGHT * (row);
    return CGRectMake(topLeft.x, topLeft.y, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
}

メタデータの各部分の画像ビューと小さな境界線を作成します。

- (UIImageView*)createImageViewFor:(MetaData*)metadata
{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:metadata.lastImage];
    imageView.frame = CGRectMake(0, 0, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);;
    imageView.layer.borderColor = [[UIColor blackColor] CGColor];
    imageView.layer.borderWidth = 2.0;
    imageView.userInteractionEnabled = YES;
    return imageView;
}

これは、ビューが作成され、親に追加される場所です...

    imageView = [self createImageViewFor:metadata];
    //[data.imageView sizeToFit];

    // Make sure the scrollView contentSize represents the data
    CGRect lastFrame = [self frameForData:data atIndex:self.data.count-1];
    CGFloat newHeight = lastFrame.origin.y + lastFrame.size.height;
    if (self.bookshelfScrollView.contentSize.height < newHeight) {
        CGSize newSize = self.bookshelfScrollView.contentSize;
        newSize.height = newHeight;
        self.bookshelfScrollView.contentSize = newSize;
    }
    [self.bookshelfScrollView addSubview:data.imageView];

したがって、各フレームを作成してビューに追加するだけで、ユーザーの操作を有効にするだけで済みます。そうしないと、スクロールビューでジェスチャが通過できなくなります。

OK...あなたが投稿したコードを見て...あなたはそれの何が悪いのかを言わなかったので...言うのは難しい...以下はあなたのコードです...私のコメントは、まあ、コメントです。 。

- (IBAction)editButtonPressed:(id)sender {
    editButton.hidden = YES;
    backToGalleryButton.hidden = NO;
    tapToDeleteLabel.hidden = NO;
}
- (IBAction)tapGesture:(UITapGestureRecognizer*)gesture
{
    // I don't think I'd do this here, but it shouldn't cause "problems."
    UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete"
                                                              message:@"Are you sure you want to delete this photo?"
                                                             delegate:self
                                                    cancelButtonTitle:@"No"
                                                    otherButtonTitles:@"Yes", nil];
    [deleteAlertView show];

    // In your controller, you have the main view, which is the view
    // on which you added your UIViews.  You need that view.  Add it as an IBOutlet
    // You should know how to do that...  ctrl-drag to the class INTERFACE source.
    // Assuming you name it "galleryView"

    // Take the tap location from the gesture, and make sure it is in the
    // coordinate space of the view.  Loop through all the imageViews and
    // find the one that contains the point where the finger was taped.
    // Then, "remove" that one from its superview...
    CGPoint tapLocation = [gesture locationInView: self.galleryView];
    for (UIImageView *imageView in self.galleryView.subviews) {
        if (CGRectContainsPoint(imageView.frame, tapLocation)) {
            [imageView removeFromSuperview];
        }
    }
}
于 2012-04-10T23:42:15.637 に答える
1

個人的には、私の小さなフォトギャラリーでは、UIImageViewコントロールをUIButtonコントロールに置き換え、ボタンの画像プロパティをの場合と同じように設定することで、ジェスチャー認識機能をすべてバイパスしましたUIImageView。見た目は同じですが、ジェスチャーをまったく必要とせずに、サムネイルを無料でタップする機能を利用できます。

したがって、私のビューにはがありますUIScrollView。これは、ボタンコントロール用に設定された画像を使用して、プログラムで画像をボタンとして追加します。例:

- (void)loadImages
{
    listOfImages = [ImageData newArrayOfImagesForGallery:nil];

    // some variables to control where I put my thumbnails (which happen to be 76 x 76px)

    int const imageWidth = 76;
    int const imageHeight = imageWidth;
    NSInteger imagesPerRow;
    NSInteger imagePadding;
    NSInteger cellWidth;
    NSInteger cellHeight;

    // some variables to keep track of where I am as I load in my images

    int row = 0;
    int column = 0;
    int index = 0;

    // add images to navigation bar

    for (ImageData *item in listOfImages)
    {
        // figure out what row and column I'm on

        imagesPerRow = self.view.frame.size.width / (imageWidth + 2);
        imagePadding = (self.view.frame.size.width - imageWidth*imagesPerRow) / (imagesPerRow + 1);
        cellWidth = imageWidth + imagePadding;
        cellHeight = imageHeight + imagePadding;

        // this is how I happen to grab my UIImage ... this will vary by implementation

        UIImage *thumb = [item imageThumbnail];

        // assuming I found it...

        if (thumb)
        {
            // create my button and put my thumbnail image in it

            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(column * cellWidth  + imagePadding, 
                                      row    * cellHeight + imagePadding, 
                                      imageWidth, 
                                      imageHeight);
            [button setImage:thumb forState:UIControlStateNormal];
            [button addTarget:self
                       action:@selector(buttonClicked:)
             forControlEvents:UIControlEventTouchUpInside];
            button.tag = index++;
            [[button imageView] setContentMode:UIViewContentModeScaleAspectFit];

            // add it to my view

            [scrollView addSubview:button];

            // increment my column (and if necessary row) counters, making my scrollview larger if I need to)

            if (++column == imagesPerRow) 
            {
                column = 0;
                row++;
                [scrollView setContentSize:CGSizeMake(self.view.frame.size.width, (row+1) * cellHeight + imagePadding)];
            }
        }
    }
}

// I also have this in case the user changes orientation, so I'll move my images around if I need to

- (void)rearrangeImages
{
    if (!listOfImages)
    {
        [self loadImages];
        return;
    }

    // a few varibles to keep track of where I am

    int const imageWidth = 76;
    int const imagesPerRow = self.view.frame.size.width / (imageWidth + 2);
    int const imageHeight = imageWidth;
    int const imagePadding = (self.view.frame.size.width - imageWidth*imagesPerRow) / (imagesPerRow + 1);
    int const cellWidth = imageWidth + imagePadding;
    int const cellHeight = imageHeight + imagePadding;

    NSArray *buttons = [[NSArray alloc] initWithArray:[scrollView subviews]];

    int row;
    int column;
    int index;

    CGRect newFrame;

    // iterate through the buttons

    for (UIView *button in buttons)
    {
        index = [button tag];

        if ([button isKindOfClass:[UIButton class]] && index < [listOfImages count])
        {
            // figure out where the button should go

            row = floor(index / imagesPerRow);
            column = index % imagesPerRow;
            newFrame = CGRectMake(column * cellWidth  + imagePadding, 
                                  row    * cellHeight + imagePadding, 
                                  imageWidth, 
                                  imageHeight);

            // if we need to move it, then animation the moving

            if (button.frame.origin.x != newFrame.origin.x || button.frame.origin.y != newFrame.origin.y)
            {
                [UIView animateWithDuration:0.33 animations:^{
                    [button setFrame:newFrame];
                }];
            }
        }
    }

    NSInteger numberOfRows = floor(([listOfImages count] - 1) / imagesPerRow) + 1;

    [scrollView setContentSize:CGSizeMake(self.view.frame.size.width, numberOfRows * cellHeight + imagePadding)];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self rearrangeImages];
}

UIButtonうまくいけば、これにより、ギャラリーで画像として機能するをプログラムで追加する方法がわかります。私はこれをその場で編集しようとしたので、プロセスにエラーが発生した場合は事前に謝罪しますが、おそらく何ができるかを理解できるはずです...これを行うためにコードを削除しました個別のGCDキュー(100近くの画像があり、メインキューでこれを行うのは遅すぎるためです。)

とにかく、次にbuttonClicked、の表示を行うためのメソッドを作成できますUIAlertView

- (void)buttonClicked:(UIButton *)sender
{
     if (inEditMode)
     {
         // create your UIAlterView using [sender tag] to know which image you tapped on
     }
}

最後に、には2つのボタンがありますUIAlertViewが、ユーザーがどのボタンをクリックしたかを確認していないようです。ビューコントローラは、実際の編集手順を実行するをUIAlterViewDelegate定義したである必要があります。alertView:clickedButtonAtIndex

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
     // ok, will do what I need to do with whatever the user tapped in my alertview
}
于 2012-04-11T00:30:21.127 に答える
0

頭のてっぺんから考えるのが最も簡単なのは、デフォルトでジェスチャを無効にし、編集ボタンを押したらジェスチャを有効にすることです。このように、画像は「編集」モードの場合にのみタップジェスチャレコグナイザーに応答します。

于 2012-04-10T23:38:41.340 に答える