You can try to press the power button to force shut down your Mac first. Then restart it after waiting a few seconds.
If a hard restart can't boot up your iMac, here are some other ways I've summarized to fix your iMac that gets stuck on the loading screen.
- Disconnect unnecessary external devices from your iMac.
- Enter Safe Mode. If your Mac enters Safe Mode, it means certain third-party apps cause this trouble.
- Disabling them in the login items list in System Preferences > Users & Groups. Or, directly uninstall these programs.
- Reset NVRAM & SMC. Resetting NVRAM is to get rid of issues in OS-related settings while resetting SMC verifies if the power issues cause the iMac won't turn on.
- Check the startup disk. Boot into macOS Recovery Mode and run Disk Utility First Aid to check the errors on the startup disk.
- Reinstall macOS. Reinstalling macOS may cause data loss. And since your iMac won't boot up, you'd better use iBoysoft Mac Recovery Mode to recover your files from the unbootable iMac first. Then, reinstall macOS in Recovery Mode.
- Contact a local repair service. If the above ways don't work, your Mac may have hardware problems.