public PhotoPuzzlePage4(ImageSource[] imageSources) { InitializeComponent(); // Loop through the rows and columns. for (int row = 0; row < NUM; row++) { for (int col = 0; col < NUM; col++) { // But skip the last one! if (row == NUM - 1 && col == NUM - 1) { break; } // Get the bitmap for each tile and instantiate it. ImageSource imageSource = imageSources[NUM * row + col]; PhotoPuzzleTile tile = new PhotoPuzzleTile(row, col, imageSource); // Add tap recognition. TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer { Command = new Command(OnTileTapped), CommandParameter = tile }; tile.GestureRecognizers.Add(tapGestureRecognizer); // Add it to the array and the AbsoluteLayout. tiles[row, col] = tile; absoluteLayout.Children.Add(tile); } } }
void OnContentViewSizeChanged(object sender, EventArgs args) { ContentView contentView = (ContentView)sender; double width = contentView.Width; double height = contentView.Height; if (width <= 0 || height <= 0) { return; } // Orient StackLayout based on portrait/landscape mode. stackLayout.Orientation = (width < height) ? StackOrientation.Vertical : StackOrientation.Horizontal; // Calculate tile size and position based on ContentView size. tileSize = Math.Min(width, height) / NUM; absoluteLayout.WidthRequest = NUM * tileSize; absoluteLayout.HeightRequest = NUM * tileSize; foreach (View view in absoluteLayout.Children) { PhotoPuzzleTile tile = (PhotoPuzzleTile)view; // Set tile bounds. AbsoluteLayout.SetLayoutBounds(tile, new Rectangle(tile.Col * tileSize, tile.Row * tileSize, tileSize, tileSize)); } }
async Task AnimateTile(int row, int col, int newRow, int newCol, uint length) { // The tile to be animated. PhotoPuzzleTile animaTile = tiles[row, col]; // The destination rectangle. Rectangle rect = new Rectangle(emptyCol * tileSize, emptyRow * tileSize, tileSize, tileSize); // Animate it! await animaTile.LayoutTo(rect, length); // Set layout bounds to same Rectangle. AbsoluteLayout.SetLayoutBounds(animaTile, rect); // Set several variables and properties for new layout. tiles[newRow, newCol] = animaTile; animaTile.Row = newRow; animaTile.Col = newCol; tiles[row, col] = null; emptyRow = row; emptyCol = col; }
async void OnTileTapped(object parameter) { if (isBusy) { return; } isBusy = true; PhotoPuzzleTile tappedTile = (PhotoPuzzleTile)parameter; await ShiftIntoEmpty(tappedTile.Row, tappedTile.Col); isBusy = false; }